如何创建Recycler View Grid分页?

时间:2015-06-25 08:11:57

标签: android android-recyclerview

我创建了Recycler View Grid。我希望实现分页。但我不明白它是怎么做的。我找到了一个答案enter link description here

但它对我不起作用。我的LayOutManager中没有方法mLayoutManager.findFirstVisibleItemPosition();。方法mRecyclerView.setOnScrollListener已弃用。如何在Recycler View Grid中实现分页?

3 个答案:

答案 0 :(得分:1)

最新答案,但是您可以尝试一下。而且效果很好

allMovie_recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            if (dy > 0) {

                val layoutManager = allMovie_recycler_view.layoutManager as GridLayoutManager
                val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition()+1
                if (visibleItemCount == layoutManager.itemCount){
                    //Load more data
                }


            }
        }
    })

答案 1 :(得分:-1)

以下是使用StaggeredGridLayoutManager进行分页的正确方法。

唯一的区别在于findFirstVisibleItemPositions(),它为每个范围内的第一个可见位置返回int[]

mRecyclerView.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) {
        super.onScrolled(recyclerView, dx, dy);
        int visibleItemCount = mGridLayoutManager.getChildCount();
        int totalItemCount = mGridLayoutManager.getItemCount();
        int[] firstVisibleItemPositions = mGridLayoutManager.findFirstVisibleItemPositions(null);

        if (!mIsLoading && !mIsLastPage) {
            if ((firstVisibleItemPositions[0] + visibleItemCount) >= totalItemCount
                && firstVisibleItemPositions[0] >= 0
                && totalItemCount >= Config.PAGE_SIZE) {
                loadMorePosts();
            }
        }
    }
});

答案 2 :(得分:-3)

我知道答案很晚。对于可能遇到此问题的其他人,请使用此解决方案

mRecyclerView.addOnScrollListener(mRecyclerViewOnScrollListener);

private RecyclerView.OnScrollListener
        mRecyclerViewOnScrollListener = 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) {
        super.onScrolled(recyclerView, dx, dy);
        int visibleItemCount = layoutManager.getChildCount();
        int totalItemCount = layoutManager.getItemCount();
        int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

        if (!isLoading && currentPage < totalPages) {
            if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
                    && firstVisibleItemPosition >= 0
                    && totalItemCount >= numberOfItemsInAPage) {
                loadMoreItems();
            }
        }
    }
};