如何知道,如果用户滚动到一个列表视图/滚动视图的顶部或底部 [英] How to know if the user has scrolled to the top or bottom of a listview/scrollview

查看:122
本文介绍了如何知道,如果用户滚动到一个列表视图/滚动视图的顶部或底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,当用户滚动到列表视图的顶部或底部,他不能再滚动。

I am trying to know when the user has scrolled to the top or bottom of the listview and he cannot scroll anymore.

现在我使用OnScrollListener知道什么列表视图项目都可见。

Now i'm using OnScrollListener to know what listview items are visible.

    listview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            }
        }

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

            if (totalItemCount - visibleItemCount == firstVisibleItem) {
                //last item visible
            }

            if (firstVisibleItem == 0) {
                //first item visible
            }
        }
    });


推荐答案

我已发现由第一或最后一个项目的检查的偏移的溶液中,这些项目的时偏移0,那么我们已经达到了底部/顶部在的ListView

I have found a solution by checking the offset of the first or the last item, when the offset of those items is 0 then we have reached the bottom/top of the listview.

    listview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem == 0) {
                // check if we reached the top or bottom of the list
                View v = listview.getChildAt(0);
                int offset = (v == null) ? 0 : v.getTop();
                if (offset == 0) {
                    // reached the top:
                    return;
                } 
            } else if (totalItemCount - visibleItemCount == firstVisibleItem){
                View v =  listview.getChildAt(totalItemCount-1);
                int offset = (v == null) ? 0 : v.getTop();
                if (offset == 0) {
                    // reached the top:
                    return;
                }
            }               
        }
    });

这篇关于如何知道,如果用户滚动到一个列表视图/滚动视图的顶部或底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆