RecyclerView.OnScrollListener和ScrollingView:我无法为滚动结束

时间:2016-08-16 17:51:05

标签: android android-recyclerview

所以我一直在阅读 Big Nerd Ranch Guide,第2版 for Android Development,我的任务是挑战"做点什么"当滚动条到达结尾时。

注意: 在描述问题之前,我想说我不会将作业委托给你。我尝试过这个问题,并在此提供我的工作以供参考

以下是完整的图片:

首先,这里是约束:

  • 实施必须使用RecyclerView
  • 因为我支持API级别16,所以我必须实现RecyclerView.OnScrollListener滚动事件处理
  • 所以我不能使用ScrollView或任何其他替代

好。问题很简单 - RecyclerView假设填充了一个Views网格,当用户向下滚动到底部时,需要使用新内容更新这些视图。 我已经实现了RecyclerView.OnScrollListener,这里是代码段:

private class PhotoScrollListener extends RecyclerView.OnScrollListener{

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy){
            //When the scrolled distance equals the extent of it, we need to load a new page
            Log.d("ScrollExtent", "dy=" + dy + "computeVerticalScrollRange()=" + mPhotoRecyclerView.computeVerticalScrollRange());

            if(dy == mPhotoRecyclerView.computeVerticalScrollRange()){
                //Reload the model object; Increment the page that needs to be retrieved
                mPageNo++;
                new FetchItemsTask().execute();
                mAdapter.notifyDataSetChanged();
            }
        }
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState){
            //STUB FOR NOW
        }
    }

此代码错误,因为dy和computeVerticalScrollRange()含义不同,因此这是一个错误。 我无法找到这些滚动方法返回偏移量和范围的单位。它也没有在Android Reference中记录。还有其他相关方法,例如computeVerticalScrollExtent()computeVerticalScrollOffset(),但我无法找到任何方法来检查滚动条是否已到达

有人可以对这些方法的工作有所启发吗?

2 个答案:

答案 0 :(得分:0)

    mPhotoRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
            int lastPosition = gridLayoutManager.findLastCompletelyVisibleItemPosition();
            // reach bottom
            if (newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition + 1 == mItems.size()) {
                new FetchItemsTask().execute();
                // updateUI: append new gallery item, you can refer to CriminalIntent
                // or simply use setAdapter() although it will lose previous page's data
                updateUI();
            }
        }
    });

答案 1 :(得分:-1)

试试这个

    int pastVisiblesItems, visibleItemCount, totalItemCount;
    private boolean loading = true;


recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }


        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            if (loading) {
                if (dy > 0) {
                    visibleItemCount = layoutManager.getChildCount();
                    totalItemCount = layoutManager.getItemCount();
                    pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        loading = false;

                        Log.v(TAG, " Reached Last Item");

                    }

                }
            }
        }
    });