The getView () adapter requests position 0 twice before each position. What for?

I read all the other posts in getView () and did not find any solutions. I have a GridView with SimpleCursorAdapter. I register (position); in getView (), and I see a pattern like this:

0,0,1,0,0,2,0,0,3,0,0,0,0,0,0,0,5 etc. This means that I need to build 3 views, as they scroll for each new view displayed, and it is intermittent and laggy. Why is he doing this? I have nothing obvious like setting my gridview to wrap-content or something else weird. There is nothing strange in my code. One thing that can be a factor is that each element can have a different height depending on the length of the displayed text.

I am currently debugging 4.2.2 Galaxy Nexus.

+6
source share
3 answers

Index 0 is requested in a grid / layout measurement session.

The question contains no details, but the following may explain the pattern you see:

  • GridView is in a layout that requires two measurement / layout passes (e.g. LinearLayout with weights, RelativeLayout with layout dependency rules). This explains the two positions of 0s.

  • Each getView() results in a re-layout of the parent. This explains position 0 after each position.

+1
source

make sure your list height is not wrap_content

and make it fill_parent.

Hope this helps.

0
source

As I wrote in the comments on the question, I noticed that getView() is called at position 0, although this position is not displayed at all. I think the reason is the need to measure elements with a grid.

It does not bother me that this happens a bit, but it bothers me when he did a lot and scrolled the grid, making the grid long. Especially when he redraws elements that are not even visible.

As I decided that when getView (0) is requested and the element 0 should not be noticed, that is, the measurement is performed, I return the actual view that was already drawn (reusing the converted view), avoiding redrawing the 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; } 
0
source

All Articles