2

我正在使用带有无尽滚动的 StaggeredGridLayoutManager 的 Recyclerview。当列表的最后一项对用户可见时,我想进行网络调用。所以这是我的代码:

   public RecyclerViewAdapter(Context context) {

            Log.d(Const.DEBUG, "RecyclerViewAdapter Constructor()");

            this.context = context;

            final StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) recyclerView.getLayoutManager();

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

                    totalItemCount = staggeredGridLayoutManager.getItemCount();
                    lastVisibleItems = staggeredGridLayoutManager.findLastVisibleItemPositions(new int[mStaggeredLayoutManager.getSpanCount()]);

                    Log.d(Const.DEBUG, "LastVisibleItems: " + Arrays.toString(lastVisibleItems));
                    Log.d(Const.DEBUG, "LastVisibleItems Count: " + lastVisibleItems.length);

                    if (staggeredGridLayoutManager.getSpanCount() == 1) {
                        lastVisibleItem = lastVisibleItems[0];
                    } else if (staggeredGridLayoutManager.getSpanCount() == 2) {
                        lastVisibleItem = Math.max(lastVisibleItems[0], lastVisibleItems[1]);
                    } else if (staggeredGridLayoutManager.getSpanCount() == 3) {
                        lastVisibleItem = Math.max(Math.max(lastVisibleItems[0], lastVisibleItems[1]), lastVisibleItems[2]);
                    }


                    if (!isRefreshing && (totalItemCount <= lastVisibleItem + visibleThreshold)) {

                        Log.d(Const.DEBUG, "isRefreshing: " + isRefreshing);
                        Log.d(Const.DEBUG, "totalItemCount: " + totalItemCount);
                        Log.d(Const.DEBUG, "lastVisibileItem: " + lastVisibleItem);
                        Log.d(Const.DEBUG, "visibileThreshold: " + visibleThreshold);

                        Log.d(Const.DEBUG, "calling LoadMore()");

                        if (mIOnLoadMoreListener != null) {
                            mIOnLoadMoreListener.onLoadMore();
                        }

                        isRefreshing = true;
                    }
                }
            });

        }

我在第一次调用时从服务器获得 9 个值。因此,当我移动到最后一个索引 [8] 时,lastVisibleItem 将为 8,visibleThreshold 为 1,totalItemCount 为 9,因此应该发生下一次网络调用。现在实际发生的情况是,当屏幕第一次加载时,lastVisibleItem 应该是 1 或 2,但它返回 8,因此会调用 loadMore()。

日志猫:

D/xx: Items Count: 9
D/xx: LastVisibleItems: [8]
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: LastVisibleItems Count: 1
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: isRefreshing: false
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: totalItemCount: 9
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: lastVisibileItem: 8
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: visibileThershold: 1
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: calling LoadMore()
03-22 15:22:52.772 5957-5957/codsiga.com.xx D/xx: onLoadMore()
03-22 15:22:52.798 5957-5957/codsiga.com.xx D/xx: getDataFromServer()

上面的代码有什么问题?需要帮助请叫我。相同的代码以前运行良好。

4

3 回答 3

4

尝试这个。

    private int pastVisiblesItems, visibleItemCount, totalItemCount;
        private boolean loading = true;   
        private StaggeredGridLayoutManager layoutManager;
        private RecyclerView recyclerView ;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            layoutManager=new StaggeredGridLayoutManager(2,1);
            recyclerView = (RecyclerView) findViewById(R.id.recycler_view);   
            recyclerView.setLayoutManager(layoutManager);
            recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                        if (dy > 0) //for vertical scrolling
                        {

                        } else if (dy < 0) {

                        }
                        if (dx > 0)//for horizontal scrolling
                        {
                           visibleItemCount = layoutManager.getChildCount();
                            totalItemCount = layoutManager.getItemCount();
                            pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                            if (loading) {
                                if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                                    loading = false;                        
                                    loadMore();  //desired function call                      
                                }
                            }
                        } 
                    }
                });
    }

API调用成功后请不要忘记设置loading=true 。

希望它可以帮助..

于 2017-03-22T10:43:40.040 回答
2

以下代码对我有用:

//return true if it's last item visited
 private boolean isLastItemDisplaying(RecyclerView recyclerView) {
    if (recyclerView.getAdapter().getItemCount() != 0) {
        //int lastVisibleItemPosition = ((StaggeredGridLayoutManager)recyclerView.getLayoutManager()).findLastVisibleItemPositions();
        int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) Objects.requireNonNull(recyclerView.getLayoutManager())).findLastVisibleItemPositions(null);
        // get maximum element within the list
        int lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
        return lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1;
    }
    return false;
}



//get last item
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;
}
于 2019-08-06T07:42:02.710 回答
1
private void recyclerViewOnScroll(RecyclerView recyclerView, final StaggeredGridLayoutManager staggeredGridLayoutManager) {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if (dy > 0) {

                    totalItemCount = staggeredGridLayoutManager.getItemCount();
                    if (lastPositions == null)
                        lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                    lastPositions = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(lastPositions);
                    lastVisibleItem = Math.max(lastPositions[0], lastPositions[1]);//findMax(lastPositions);

                    if (!loading && totalItemCount >= 20 && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        // End has been reached
                        if (onLoadMoreListener != null) {
                            onLoadMoreListener.onLoadMore();
                        }
                        loading = true;
                    }
                }
            }
        });
    }
于 2017-10-09T10:38:28.440 回答