本文介绍了GridView中获取浏览者的位置,第一个孩子查看不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的的GridView 是挺有意思的,它重新使用的子视图。那些滚动了来自底层回来。因此,有没有从的GridView 方法来获取其位置的子视图。但我真的需要通过它的位置来获得观,做它的一些工作。因此,要做到这一点,我创建了一个 SparseArray getView 把<$ C $发表意见,他们在其中的位置C> BaseAdapter 。

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);
}

现在,我可以通过自己的位置所有可见的美景。一切都运行完美,因为它应该,但在某些设备上,第一个孩子视图(位置0)不一样的一个数组。我登录了 getView ,发现卡位0, getView 得到了所谓的很多次,每一次阵列设置不同视图。我不知道为什么GridView的呼吁 getView 为0位置很多次,只发生在少数设备。任何解决方案?

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 ?

推荐答案

阅读源 getPositionForView 之后,我已经写了这个方法,在自己的GridView,通过API 18运行完美

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);
}

这篇关于GridView中获取浏览者的位置,第一个孩子查看不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:26