My Activity consists of GridView which holds 40+ elements. After starting activity user could see maximum 15 items (3 rows, 5 items in each row). I wrote in getView() body to pa
For me, it's even worse: 0 position is called often 10 times. And it's called even if I'm at the end of the list, without position 0 on the screen.
My solution is a static ArrayList of items being currently processed for display and I ignore those double-calls. It's not the best solution though as it still uses resources to get 0 position while it's even not on the screen.
As it seems like a bug, I think I will cache a View for position 0 so it would get a quick and proper return value even if the position is not on the screen.
Android 4.2.2.
I had the same Problem, my solution was to create a List outside the Adapter, then give the List to the Adapter, something like that:
class MyAdapter extends BaseAdapter{
List<View> views;
public MyAdapter(List<View> views){
this.views = views;
}
...
public View getView(int position, View convertView, ViewGroup parent){
return views.get(position); // now your safe even if element[0] is called n times
}
}
class MyActivity extends Activity{
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<View> views = create_your_views():
MyAdapter adapter = new MyAdapter(views);
grid.setAdapter(adapter);
}
}
The response on that link if quite clear I think
There is absolutely no guarantee on the number of times getView() will be invoked for any given position
Now this remark also implies you shouldn't use wrap_content
usually occurs with lists and grids that have a height set to wrap_content
But that does not mean it would not happen for other situations. The bug was closed as "WorkingAsIntended", so I don't think you can do too much. It's just something that can happen.