Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 228
Steve Gehrman Avatar asked Jan 23 '14 07:01

Steve Gehrman


People also ask

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });

How to edit ListView item in android?

OnItemClickListener MshowforItem = new AdapterView. OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((TextView)view). setText("Hello"); } };


1 Answers

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.

like image 190
laalto Avatar answered Oct 04 '22 17:10

laalto