GridView get view by position, first child view different

Viewed 10013

Android GridView is quite interesting, it reuses the child views. The ones scrolled up comes back from bottom. So there is no method from GridView to get the child view by its position. But I really need to get view by its position and do some work on it. So to do that, I created an SparseArray and put views by their position in it from getView of BaseAdapter.

SparseArray<View> ViewArray = new SparseArray<View>();

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;    
    if (view == null)
       view = li.inflate(layoutID, null);
    ViewArray.put(position, view);
}

Now, I can get all visible views by their position. Everything works perfect as it should but in some devices, the first child view(position 0) is not same as the one in array. I logged the getView and found that for position 0, getView got called many times and each time array was set with different view. I have no idea why GridView is calling getView for position 0 many times and that happens only on few devices. Any solution ?

3 Answers
Related