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

3

6

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.

Cedilla answered 23/1, 2014 at 7:58 Comment(4)
Are you using any AsynchTask in your getView()??Ordovician
please post your code .Upbear
somewhat same problem here and everywhere else. Android's getView() method of adapter has always been a mess.Doradorado
I want to elaborate that getView() on position 0 is invoked even when position 0 isn't seen at all, like when the grid is scrolled down. I guess this happens because Android wants to measure the grid.Prayer
E
1

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.

Electrostriction answered 23/1, 2014 at 8:2 Comment(1)
I'm seeing the same thing. I changed the layout to match_parent without success.Prayer
U
0

make sure that the height of your listview is not wrap_content

and make it fill_parent .

Hope that helps .

Upbear answered 23/1, 2014 at 8:9 Comment(0)
P
0

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;
}
Prayer answered 21/11, 2014 at 4:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.