本文介绍了ListView控件(适配器)项目可见监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能对每个ListView的项目可视性监听器?这样的:用户看到的项目,做一些事情。在项目被隐藏,做别的事情。

我要检查时,项进入和退出滚动以便更新第二列表。

Additionaly我的ListView可能会扩大,如:

  adapter.addAll((收集和LT ;?延伸DBOBJECT>)事件);
adapter.notifyDataSetChanged();

我想办法可能是使用一个全球性的监听器,检查项目,但恐怕它会被搞砸如果我添加更多的项目到ListView(上图)。我还没有与一事无成尚未

  eventList.setOnScrollListener(新OnScrollListener(){        公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,诠释totalItemCount){        }        @覆盖
        公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){}
});


解决方案

您可以使用onScroll功能来计算。

  eventList.setOnScrollListener(新OnScrollListener(){
    INT oldFirstVisibleItem = 0;
    INT oldLastVisibleItem = 0;    公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,诠释totalItemCount){
        如果(firstVisibleItem> oldFirstVisibleItem){
            的for(int i = oldFirstVisibleItem; I< firstVisibleItem;我++){
                onExit(ⅰ);
            }
        }
        如果(firstVisibleItem< oldFirstVisibleItem){
            的for(int i = firstVisibleItem; I< oldFirstVisibleItem;我++){
                的OnEnter(ⅰ);
            }
        }        INT lastVisibleItem = firstVisibleItem + visibleItemCount - 1;
        如果(lastVisibleItem< oldLastVisibleItem){
            的for(int i = oldLastVisibleItem + 1; I< = lastVisibleItem;我++){
                onExit(ⅰ);
            }
        }
        如果(lastVisibleItem> oldLastVisibleItem){
            的for(int i = oldLastVisibleItem + 1; I< = lastVisibleItem;我++){
                的OnEnter(ⅰ);
            }
        }        oldFirstVisibleItem = firstVisibleItem;
        oldLastVisibleItem = lastVisibleItem;
    }    @覆盖
    公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){}
});公共无效的OnEnter(INT位置){
    //处理项目进入人们的视野。
}公共无效onExit(INT位置){
    //处理走出去的观点的项目。
}

Is it possible to have a visibility listener on each listview's item? Like: on user sees the item, do something. On item is hidden, do something else.

I want to check when a item "enters" or "exits" the scroll so as to update a second list.

Additionaly my ListView might expand like:

adapter.addAll(  (Collection<? extends DBObject>) events);
adapter.notifyDataSetChanged();

I think a way might be to use a global listener to check the items but I am afraid it will get messed up if I add more items to the ListView (above). I have not accomplished anything with that yet

eventList.setOnScrollListener(new OnScrollListener() {

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {}
});
解决方案

You can use the onScroll function to calculate that.

eventList.setOnScrollListener(new OnScrollListener() {
    int oldFirstVisibleItem = 0;
    int oldLastVisibleItem = 0;

    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem > oldFirstVisibleItem) {
            for(int i = oldFirstVisibleItem; i < firstVisibleItem; i++) {
                onExit(i);
            }
        }
        if (firstVisibleItem < oldFirstVisibleItem) {
            for(int i = firstVisibleItem; i < oldFirstVisibleItem; i++) {
                onEnter(i);
            }
        }

        int lastVisibleItem = firstVisibleItem + visibleItemCount - 1;
        if (lastVisibleItem < oldLastVisibleItem) {
            for(int i = oldLastVisibleItem+1; i <= lastVisibleItem; i++) {
                onExit(i);
            }
        }
        if (lastVisibleItem > oldLastVisibleItem) {
            for(int i = oldLastVisibleItem+1; i <= lastVisibleItem; i++) {
                onEnter(i);
            }
        }

        oldFirstVisibleItem = firstVisibleItem;
        oldLastVisibleItem = lastVisibleItem;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {}
});

public void onEnter(int position) {
    // Handle an item coming into view.
}

public void onExit(int position) {
    // Handle an item going out of view.
}

这篇关于ListView控件(适配器)项目可见监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:21