2

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

            @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 库来获得粘性标题视图。我想知道这可能是问题所在

4

3 回答 3

2

向您的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

于 2016-01-07T12:37:38.147 回答
2
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);
于 2016-02-09T09:27:56.770 回答