Doist / RecyclerViewExtensions

RecyclerView made easier.
MIT License
493 stars 42 forks source link

layoutManager.findLastVisibleItemPosition() return only the position of StickyHeader #22

Closed toe-lie closed 6 years ago

toe-lie commented 6 years ago

I would like to implement loadMore function and I added onScrollListener with the following code.

sampleRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                StickyHeadersLinearLayoutManager layoutManager = (StickyHeadersLinearLayoutManager)
                        recyclerView.getLayoutManager();
                int lastPosition = layoutManager
                        .findLastVisibleItemPosition();

                if (lastPosition == sampleAdapter.get().getItemCount() - 1) {
                    sampleViewModel.loadNextPage();
                }
            }
        });

The problem is that "layoutManager.findLastVisibleItemPosition()" method return only the position of StickHeader, not the position of last visible list item. When I tested with the default LinearLayoutManager, the above code is working proper. Is there anyway to get last visible item position (not StickyHeader position)?

toe-lie commented 6 years ago

Sorry, I'm not sure the output position is the StickHeader position or not. Below are the logs when I do the scrolling. Please check and help. Thank you.

LastPosition 7 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 9 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 2 ; ItemCount 52 LastPosition 9 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 0 ; ItemCount 52 LastPosition 7 ; ItemCount 52

goncalossilva commented 6 years ago

It's very likely that the sticky header is messing things up. Could you try overriding findLastVisibleItemPosition in StickyHeadersLinearLayoutManager and detach before / attach after calling super's method? In sum, what most of the others are doing.

toe-lie commented 6 years ago

Wow ... It work like a charm. Thanks a lot for your help, bro @goncalossilva .