使用 SuperSLiM 库检测 RecyclerView 的 GridLayoutManager 滚动结束

Detect RecyclerView's GridLayoutManager End Of Scroll with SuperSLiM library

我已经搜索了一段时间,但找不到使用网格布局管理器检测回收器视图滚动结束的解决方案。使用下面的代码确实有效,但它适用于线性布局管理器,不适用于网格布局管理器。

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
                final int treeshold = 0;
                try {
                    if (scrollState == RecyclerView.SCROLL_STATE_IDLE) {
                        if (((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition() >= yourData.size()
                                - 1 - treeshold) {
                            //your load more logic
                        }
                    }
                } catch (Exception e) {
                }
            }

我的想法是我想为我的应用程序加载更多功能,所以我需要检测滚动结束。

编辑:问题可能不是网格视图本身。我使用 com.tonicartos.superslim 库来获得粘性 header 视图。我想知道这可能是问题所在

向您的 RecyclerView 添加滚动侦听器,如下所示:

int pastVisiblesItems, visibleItemCount, totalItemCount;  
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);
                            visibleItemCount = layoutManager.getChildCount();
                            totalItemCount = layoutManager.getItemCount();
                            pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                           if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                                //bottom of recyclerview
                          }
                        }
    });

layoutmanager 是您的 GridLayoutManager

我刚刚就这个问题直接给Artos先生发了邮件,得到了回复: "It likely won't work properly until the next version is finished. I wrote about it on my blog at http://www.tonicartos.com/2015/12/superslim-milestone-1_23.html?m=1"

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();

    private int previousTotal = 0; // The total number of items in the dataset after the last load
    private boolean loading = true; // True if we are still waiting for the last set of data to load.
    private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
    int firstVisibleItem, visibleItemCount, totalItemCount;

    private int currentPage = 1;

    private RecyclerView.LayoutManager mLayoutManager;
    private boolean isUseLinearLayoutManager;
    private boolean isUseGridLayoutManager;

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.mLayoutManager = linearLayoutManager;
        isUseLinearLayoutManager = true;

    }

    public EndlessRecyclerOnScrollListener(GridLayoutManager gridLayoutManager) {
        this.mLayoutManager = gridLayoutManager;
        isUseGridLayoutManager = true;

    }

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

        visibleItemCount = recyclerView.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();


        if(isUseLinearLayoutManager && mLayoutManager instanceof LinearLayoutManager){
            firstVisibleItem = ((LinearLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
        }

        if(isUseGridLayoutManager && mLayoutManager instanceof GridLayoutManager){
            firstVisibleItem = ((GridLayoutManager) mLayoutManager).findFirstVisibleItemPosition();
        }

        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount)
                <= (firstVisibleItem + visibleThreshold)) {
            // End has been reached

            // Do something
            currentPage++;

            onLoadMore(currentPage);

            loading = true;
        }
    }

    public abstract void onLoadMore(int currentPage);