检测RecyclerView在滚动时何时到达最底部位置

Adr*_*ian 65 android scroll android-recyclerview

我有一个RecyclerView的代码.

    recyclerView = (RecyclerView)rootview.findViewById(R.id.fabric_recyclerView);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(new RV_Item_Spacing(5));
    FabricAdapter fabricAdapter=new FabricAdapter(ViewAdsCollection.getFabricAdsDetailsAsArray());
    recyclerView.setAdapter(fabricAdapter);
Run Code Online (Sandbox Code Playgroud)

我需要知道RecyclerView在滚动时何时到达最底部位置.可能吗 ?如果有,怎么样?

小智 147

还有一种简单的方法可以做到这一点

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

        if (!recyclerView.canScrollVertically(1)) {
            Toast.makeText(YourActivity.this, "Last", Toast.LENGTH_LONG).show();

        }
    }
});
Run Code Online (Sandbox Code Playgroud)

方向整数:-1表示向上,1表示向下,0表示总是返回false.

  • onScrolled()可防止检测发生两次. (12认同)
  • 在`if`语句中添加`newState == RecyclerView.SCROLL_STATE_IDLE`。 (2认同)

Feb*_*lix 38

只需在recyclerview上实现addOnScrollListener()即可.然后在滚动侦听器内部实现下面的代码.

RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (mIsLoading)
                return;
            int visibleItemCount = mLayoutManager.getChildCount();
            int totalItemCount = mLayoutManager.getItemCount();
            int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
            if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                //End of list
            }
        }
    };
Run Code Online (Sandbox Code Playgroud)

  • 无法在`android.support.v7.widget.RecyclerView`上解析方法`'findFirstVisibleItemPosition'` (3认同)
  • @Iman Marashi,您需要强制转换:`(recyclerView.layoutManager为LinearLayoutManager).findFirstVisibleItemPosition()。这项工作。 (2认同)

dak*_*aka 15

答案在Kotlin中,它将在Java中运行.如果您复制和粘贴,IntelliJ应该为您转换它.

recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

        // 3 lines below are not needed.
        Log.d("TAG","Last visible item is: ${gridLayoutManager.findLastVisibleItemPosition()}")
        Log.d("TAG","Item count is: ${gridLayoutManager.itemCount}")
        Log.d("TAG","end? : ${gridLayoutManager.findLastVisibleItemPosition() == gridLayoutManager.itemCount-1}")

        if(gridLayoutManager.findLastVisibleItemPosition() == gridLayoutManager.itemCount-1){
            // We have reached the end of the recycler view.
        }

        super.onScrolled(recyclerView, dx, dy)
    }
})
Run Code Online (Sandbox Code Playgroud)

这也适用,LinearLayoutManager因为它具有上面使用的相同方法.即findLastVisibleItemPosition()getItemCount()(itemCount在科特林).

  • 好的方法,但是一个问题是if循环中的语句将执行多次 (3认同)

Mil*_*ary 13

使用此代码避免重复调用

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

            if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
                Log.d("-----","end");

            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 我尝试过可能会回答,但这对我来说是简单而完美的。谢谢 (2认同)
  • 各位小心!当您尝试向上滑动而回收器中的元素很少时,也会触发此操作。它还将触发两次有关 newState 被忽略的事件。 (2认同)
  • 只需添加一个额外的检查来确定是否可以向上滚动。如果您无法向上滚动,也无法向下滚动,则忽略结果(因为列表不够长)。`recyclerView.canScrollVertically(-1) && !recyclerView.canScrollVertically(1)` (2认同)

Man*_*ath 12

我没有通过上述答案得到完美的解决方案,因为它甚至在 onScrolled

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
           if( !recyclerView.canScrollVertically(RecyclerView.FOCUS_DOWN))
               context?.toast("Scroll end reached")
        }
Run Code Online (Sandbox Code Playgroud)

  • 但是 `recyclerView.canScrollVertically(direction)` 方向只是任意整数 + vs - 所以如果它在底部,它可以是 4,如果它在顶部,它可以是 -12。 (2认同)

noo*_*392 10

在对这个线程中的大多数其他答案不满意之后,我发现了一些我认为更好的东西,而且这里没有任何地方。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (!recyclerView.canScrollVertically(1) && dy > 0)
        {
             //scrolled to BOTTOM
        }else if (!recyclerView.canScrollVertically(-1) && dy < 0)
        {
            //scrolled to TOP
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

这很简单,当您滚动到顶部或底部时,在所有条件下都会命中一次。


Sun*_*nil 6

尝试这个

我已经使用了上面的答案,当你在回收站视图结束时它总是运行,

如果您只想检查一次是否在底部? 示例:-如果我每次进入底部时都有 10 个项目的列表,它会再次显示我,如果我从上到下滚动,它不会再次打印,如果您添加更多列表并转到那里,它会再次显示。

注意:-在处理命中 API 中的偏移量时使用此方法

  1. 创建一个名为 EndlessRecyclerViewScrollListener 的类

        import android.support.v7.widget.GridLayoutManager;
        import android.support.v7.widget.LinearLayoutManager;
        import android.support.v7.widget.RecyclerView;
        import android.support.v7.widget.StaggeredGridLayoutManager;
    
        public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
            // The minimum amount of items to have below your current scroll position
            // before loading more.
            private int visibleThreshold = 5;
            // The current offset index of data you have loaded
            private int currentPage = 0;
            // The total number of items in the dataset after the last load
            private int previousTotalItemCount = 0;
            // True if we are still waiting for the last set of data to load.
            private boolean loading = true;
            // Sets the starting page index
            private int startingPageIndex = 0;
    
            RecyclerView.LayoutManager mLayoutManager;
    
            public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
                this.mLayoutManager = layoutManager;
            }
    
        //    public EndlessRecyclerViewScrollListener() {
        //        this.mLayoutManager = layoutManager;
        //        visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
        //    }
    
            public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) {
                this.mLayoutManager = layoutManager;
                visibleThreshold = visibleThreshold * layoutManager.getSpanCount();
            }
    
            public int getLastVisibleItem(int[] lastVisibleItemPositions) {
                int maxSize = 0;
                for (int i = 0; i < lastVisibleItemPositions.length; i++) {
                    if (i == 0) {
                        maxSize = lastVisibleItemPositions[i];
                    }
                    else if (lastVisibleItemPositions[i] > maxSize) {
                        maxSize = lastVisibleItemPositions[i];
                    }
                }
                return maxSize;
            }
    
            // This happens many times a second during a scroll, so be wary of the code you place here.
            // We are given a few useful parameters to help us work out if we need to load some more data,
            // but first we check if we are waiting for the previous load to finish.
            @Override
            public void onScrolled(RecyclerView view, int dx, int dy) {
                int lastVisibleItemPosition = 0;
                int totalItemCount = mLayoutManager.getItemCount();
    
                if (mLayoutManager instanceof StaggeredGridLayoutManager) {
                    int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null);
                    // get maximum element within the list
                    lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
                } else if (mLayoutManager instanceof GridLayoutManager) {
                    lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                } else if (mLayoutManager instanceof LinearLayoutManager) {
                    lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                }
    
                // If the total item count is zero and the previous isn't, assume the
                // list is invalidated and should be reset back to initial state
                if (totalItemCount < previousTotalItemCount) {
                    this.currentPage = this.startingPageIndex;
                    this.previousTotalItemCount = totalItemCount;
                    if (totalItemCount == 0) {
                        this.loading = true;
                    }
                }
                // If it’s still loading, we check to see if the dataset count has
                // changed, if so we conclude it has finished loading and update the current page
                // number and total item count.
                if (loading && (totalItemCount > previousTotalItemCount)) {
                    loading = false;
                    previousTotalItemCount = totalItemCount;
                }
    
                // If it isn’t currently loading, we check to see if we have breached
                // the visibleThreshold and need to reload more data.
                // If we do need to reload some more data, we execute onLoadMore to fetch the data.
                // threshold should reflect how many total columns there are too
                if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
                    currentPage++;
                    onLoadMore(currentPage, totalItemCount, view);
                    loading = true;
                }
            }
    
            // Call this method whenever performing new searches
            public void resetState() {
                this.currentPage = this.startingPageIndex;
                this.previousTotalItemCount = 0;
                this.loading = true;
            }
    
            // Defines the process for actually loading more data based on page
            public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);
    
        }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 像这样使用这个类

         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.addOnScrollListener(new EndlessRecyclerViewScrollListener( linearLayoutManager) {
                @Override
                public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
                    Toast.makeText(getActivity(),"LAst",Toast.LENGTH_LONG).show();
                }
            });
    
    Run Code Online (Sandbox Code Playgroud)

它在我的最后运行完美,如果您遇到任何问题,请评论我


can*_*ler 5

科特林答案

您可以使用此 Kotlin 函数作为底部滚动跟随最佳实践,以创建无限无限滚动。

// Scroll listener.
private fun setupListenerPostListScroll() {
    val scrollDirectionDown = 1 // Scroll down is +1, up is -1.
    var currentListSize = 0

    mRecyclerView.addOnScrollListener(
        object : RecyclerView.OnScrollListener() {

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)

                if (!recyclerView.canScrollVertically(scrollDirectionDown)
                    && newState == RecyclerView.SCROLL_STATE_IDLE
                ) {
                    val listSizeAfterLoading = recyclerView.layoutManager!!.itemCount

                    // List has more item.
                    if (currentListSize != listSizeAfterLoading) {
                        currentListSize = listSizeAfterLoading

                        // Get more posts.
                        postListScrollUpAction(listSizeAfterLoading)
                    }
                    else { // List comes limit.
                        showToastMessageShort("No more items.")
                    }
                }
            }
        })
}
Run Code Online (Sandbox Code Playgroud)