0

我想尝试延迟加载recyclerViewby staggeredGridLayoutManager,但该FindFirstVisibleItemPositions函数找不到第一个可见项目位置,它总是返回 0 和 1(我有 2 列),并且onScrolled只会在init.

我用androidx.recyclerview.widget.

我的代码

StaggeredGridLayoutManager m = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(m);
recyclerView.setAdapter(new MyAdapter());
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                            @Override
                            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                                super.onScrollStateChanged(recyclerView, newState);
                                int visibleItemCount = m.getChildCount();;
                                int totalItemCount = m.getItemCount();
                                int[] firstVisibleItemPosition = m.findFirstVisibleItemPositions(null);
                                Log.i("DebugMessage", "-----------onScrollStateChanged-----------");
                                Log.i("DebugMessage", "newState: " + newState);
                                Log.i("DebugMessage","visibleItemCount:" + visibleItemCount);
                                Log.i("DebugMessage","totalItemCount:" + totalItemCount);
                                for(int i = 0;i<firstVisibleItemPosition.length;i++)
                                {
                                    Log.i("DebugMessage","firstVisibleItemPosition:" + firstVisibleItemPosition[i]);
                                }
                            }

                            @Override
                            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                                super.onScrolled(recyclerView, dx, dy);
                                Log.i("DebugMessage", "-----------onScrolled-----------");
                                Log.i("DebugMessage", "dx: " + dx);
                                Log.i("DebugMessage", "dy: " + dy);
                            }
                        });

结果

2019-06-06 13:21:36.700 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrolled-----------
2019-06-06 13:21:36.700 26282-26282/com.sample.lazyloading I/DebugMessage: dx: 0
2019-06-06 13:21:36.700 26282-26282/com.sample.lazyloading I/DebugMessage: dy: 0
2019-06-06 13:21:47.793 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:47.794 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:47.794 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:47.794 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:47.794 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:47.794 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:48.482 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:48.482 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:21:48.482 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:48.482 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:48.482 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:48.482 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:51.779 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:51.779 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:51.779 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:51.779 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:51.780 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:51.780 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:52.399 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:52.399 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:21:52.399 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:52.400 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:52.400 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:52.400 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:53.086 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:53.086 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:53.086 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:53.087 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:53.087 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:53.087 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:53.563 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:53.563 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:21:53.563 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:53.563 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:53.564 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:53.564 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:54.218 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:54.218 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:54.219 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:54.219 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:54.219 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:54.219 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:54.702 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:54.703 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:21:54.703 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:54.703 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:54.703 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:54.703 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:55.508 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:55.508 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:55.508 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:55.508 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:55.508 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:55.508 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:55.931 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:55.931 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:21:55.931 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:55.931 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:55.931 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:55.931 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:56.531 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:56.531 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:56.531 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:56.531 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:56.532 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:56.532 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:56.993 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:56.993 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:21:56.993 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:56.993 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:56.993 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:56.993 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:57.096 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:57.097 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:21:57.097 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:57.097 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:57.097 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:57.097 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:57.496 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:57.496 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:57.496 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:57.496 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:57.496 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:57.496 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:57.592 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:57.592 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:21:57.592 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:57.592 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:57.593 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:57.593 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:58.060 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:58.061 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:58.061 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:58.061 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:58.061 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:58.061 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:58.133 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:58.133 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:21:58.133 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:58.133 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:58.133 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:58.133 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:58.696 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:58.696 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:58.696 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:58.696 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:58.696 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:58.697 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:58.768 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:58.768 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:21:58.768 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:58.768 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:58.768 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:58.768 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:59.437 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:59.437 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:21:59.437 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:59.437 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:59.437 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:59.437 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:21:59.519 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:21:59.519 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:21:59.519 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:21:59.519 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:21:59.519 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:21:59.519 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:00.013 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:00.013 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:22:00.013 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:00.013 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:00.013 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:00.013 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:00.087 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:00.088 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:22:00.088 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:00.088 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:00.088 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:00.088 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:00.590 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:00.591 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:22:00.591 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:00.591 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:00.591 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:00.591 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:00.665 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:00.665 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:22:00.665 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:00.665 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:00.665 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:00.665 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:02.176 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:02.176 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:22:02.176 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:02.176 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:02.176 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:02.176 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:14.009 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:14.009 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 1
2019-06-06 13:22:14.009 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:14.009 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:14.009 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:14.009 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:14.444 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:14.444 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 2
2019-06-06 13:22:14.445 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:14.445 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:14.445 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:14.445 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
2019-06-06 13:22:14.825 26282-26282/com.sample.lazyloading I/DebugMessage: -----------onScrollStateChanged-----------
2019-06-06 13:22:14.825 26282-26282/com.sample.lazyloading I/DebugMessage: newState: 0
2019-06-06 13:22:14.825 26282-26282/com.sample.lazyloading I/DebugMessage: visibleItemCount:30
2019-06-06 13:22:14.825 26282-26282/com.sample.lazyloading I/DebugMessage: totalItemCount:30
2019-06-06 13:22:14.825 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:0
2019-06-06 13:22:14.825 26282-26282/com.sample.lazyloading I/DebugMessage: firstVisibleItemPosition:1
4

1 回答 1

0

我找到了原因,因为我在recycleview 父级使用了nestedscrollview。

于 2019-06-12T03:47:09.007 回答