Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

android - GridView get view by position, first child view different

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After reading source of getPositionForView, I have wrote this method in own GridView, works perfect by API 18

public View GetViewByPosition(int position) {
    int firstPosition = this.getFirstVisiblePosition();
    int lastPosition = this.getLastVisiblePosition();

    if ((position < firstPosition) || (position > lastPosition))
        return null;

    return this.getChildAt(position - firstPosition);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...