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.

Steve Gehrman
  • 76
  • 1
  • 5
  • Are you using any AsynchTask in your getView()?? – kalyan pvs Jan 23 '14 at 07:59
  • please post your code . –  Jan 23 '14 at 08:10
  • somewhat same problem [here](http://stackoverflow.com/q/12561295/2389078) and everywhere else. Android's `getView()` method of adapter has always been a mess. – DroidDev Jan 23 '14 at 08:40
  • 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. – AlikElzin-kilaka Nov 21 '14 at 04:07

3 Answers3

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

make sure that the height of your listview is not wrap_content

and make it fill_parent .

Hope that helps .

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;
}
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277