RecyclerView StaggeredGridLayoutManager FindFirstVisibleItemPositions всегда равен 0 (androidx) - PullRequest
0 голосов
/ 06 июня 2019

Я хочу попробовать ленивую загрузку в recyclerView на 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

1 Ответ

0 голосов
/ 12 июня 2019

Я нахожу причину, потому что я использую nestedscrollview у родительского элемента recycleview.

...