Custom view loses background resource when not in view - android

So, I'm adding an ad from either AdMob or MMAds, OR I have the MMAds Interstitial ad come up, and on exit, OR I touch one of the list items, starting a new activity, then at the end of the activity, this happens:
http://i.imgur.com/Zq7vvkQ.png
The top item doesn't have a background resource being drawn, the bottom is similar to this. This happens sometimes to the ones in the middle (With the ads, they ALL go away).
I was wondering why this happens and how to prevent it... The most I could find on other questions is one on using the following code on the ListView:
android:cacheColorHint="#00000000"
Or
android:scrollingCache="false"
The background resource is being drawin in the custom ListAdapter's getView(int, View, ViewGroup) method.
Any help is appreciated,
Justin W.

To verify your problem try to run hierarchyviewer, analize your views tree and try to find potential solution. Have you used Hierarchy Viewer?

So, I had a bit of code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
item = inflater.inflate(R.layout.griddler_menu_choice_item, parent, false);
} else {
item = convertView;
}
return item;
}
Basically this was to keep it more efficient so it didn't have to keep creating Views as it's costly, but this somehow effected the end result... So now it works =).
Forgot to add in how to fix this... remove the else part and the if in general, just leave the line of code:
item = inflater.inflate(R.layout.griddler_menu_choice_item, parent, false);
That fixed it for me.

Related

Achive Scroll without scrollView

Yesterday I attain one interview where he asked one question :
if in a layout one banner is there and bottom of this banner ListView is there. without using scroll view how you will scroll the screen.
Edited:
Suppose In the xml file Linear layoutis there. Then add banner into the linear layout(in ImageView), then ListView is there. Now scroll the screen. How it will scroll?
I think you can do the same with the help of the getView() method of Custom adapter.
like the below:
public View getView(int position, View convertView, ViewGroup parent){
View view= convertView;
if(view==null){
if(position==0){
view=inflater.inflate(R.layout.imageViewLayout, null);
}else{
view=inflater.inflate(R.layout.otherLayoutForListItem, null);
}
}else{
}
}
I think this can help you
but if you don't want to include in this way than u have to handle some other scroll option of the list View

How to get a ListView's child view at specific index

I need to dynamically change the background color of a child view of a ListView at a specific index. So I need to get the view first before applying the background.
I have tried the following.
getViewAt(int index) - this is not applicable to ListView.
getChildAt(int index) - this gives runtime NPE error
A Google search returns irrelevant results.
Please help me out. Thank you.
I figured out how to do this.
I really think that one should not have to post code and logcat for a question that is similar to "How to set background image".
Anyway my answer is, in short, set position as tag for every child in the listview through your adapter's getView() and then get any child using findViewWithTag(Object tag).
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = (LinearLayout)inflater.inflate(R.layout.some_layout, parent, false); .
convertView.setTag(String.valueOf(position));
}
And whenever you need to get a specific child.
View v = mylistview.findViewWithTag(String.valueOf(index));
To change background color.
v.setBackgroundResource(R.color.some_color);
Use getChildAt(index) from ViewGroup to get View.

Adapter's getView() asks for position 0 twice before each postion. Why?

I read all the other posts on getView() and didn't find any solutions. I have a GridView with a SimpleCursorAdapter. I log(position); in getView() and I see a pattern like this:
0,0,1,0,0,2,0,0,3,0,0,4,0,0,5 etc. This means I'm having to build 3 views as it scrolls for every new view displayed and it's choppy and laggy. Why does it do this? I don't have anything obvious like setting my gridview to wrap-content or anything else weird. There's nothing strange about my code. One thing that might be a factor is that every item view could have a different height depending on the length of the text I'm displaying.
I'm currently debugging on a 4.2.2 Galaxy Nexus.
Index 0 is requested in gridview measure/layout pass.
The question doesn't have the details, but the following could explain the pattern you're seeing:
The GridView is in a layout that requires two measure/layout passes (e.g. LinearLayout with weights, RelativeLayout with layout dependency rules). This explains the two position 0s.
Each getView() causes the parent to re-layout. This explains the position 0 after each position.
make sure that the height of your listview is not wrap_content
and make it fill_parent .
Hope that helps .
As I wrote in the question's comments, I noticed that getView() is invoked on position 0 even though that position is NOT seen at all. I think the reason is the need to measure the items by the grid.
It doesn't bother me that much that this happens a bit, but it bothers me when it's done a lot and when scrolling down the grid, making the grid leggy. Especially when it repaints elements that aren't even seen.
The way I solved it is that when getView(0) is requested and item 0 should not be seen, meaning a measurement is done, I return an actual view which was already painted (reusing the converted view), avoiding repainting position 0.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Workaround Android requesting position 0 even though it doesn't draw it.
AbsListView listView = (AbsListView) parent;
int firstVisiblePosition = listView.getFirstVisiblePosition();
if (position == 0 && firstVisiblePosition > 10) {
if (convertView == null) {
convertView = actualPaint(position, convertView, parent);
}
return convertView;
}
convertView = actualPaint(position, convertView, parent);
return convertView;
}

Android HorizontalScrolling laggy

I have a HorzontalScrollView with a LinearLayout inside. During Runtime I can add more LinearLayouts to the LinearLayout.
Now I have the problem that the Scrollview only scrolls a little bit and not smooth with one finger slide!
Does anyone have a solution for this problem?
HorizontalScrollView doesn't use an adapter that manages the list's memory, therefore it can't handle heavy (images, custom views, etc) lists.
You can use this Horizontal ListView http://www.dev-smart.com/archives/34 but make sure you don't write the on list item click method inside the getView, it will make the list scroll slow. Other than that, that's a great resource for a smooth horizontal list view.
You can also explore the Android view pager, which is also supported on lower Android versions using the compatibility pack: http://developer.android.com/sdk/compatibility-library.html
Edit - something like that in the adapter that inflates the XML you want (the linearLayout) and then populates every view with the relevant data.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_friends_list_item, null);
}
ImageView status = (ImageView)convertView.findViewById(R.id.status);
ImageView image = (ImageView)convertView.findViewById(R.id.image);
ImageView imageBorder = (ImageView)convertView.findViewById(R.id.image_border);
TextView title = (TextView)convertView.findViewById(R.id.title);
}
The problem was my parent Viewflow because it has stolen the swipe event! The HorizontalListView is too buggy for me! (Problems with size attributes)
However,thank you for your answer! ;)

Problem with custom views in Gallery (false focus received by component)

I'm having problem figuring out an issue with the Gallery component. I'm using a simple gallery with a custom Base Adapter. In the getView method, the view representing a gallery item is inflated:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
View promo = inflater.inflate(R.layout.view_promo, null);
ImageView promoBackground = (ImageView)promo.findViewById(R.id.promo_background);
promoBackground.setBackgroundResource(promoResources[position]);
convertView = promo;
}
return convertView;
}
The inflated view also has two simple button with a selector set as background. The problem is that when the gallery item is clicked/touched, the two buttons appear to receive focus, and the selector changes the background of the two buttons (even if the touch not occurs right over them). I've tried disabling both focus and click on all views that could receive the event, but no luck yet.
Any idea is welcomed!
Thanks
I just encountered this same issue, and found a solution here:
Gallery/AdapterView Child Drawable State

Categories

Resources