I am making a movie list with RecyclerView and ConcatAdapter.
The ConcatAdapter consists of three different adapters, HeaderAdapter, MovieAdapter and FooterAdapter.
HeaderAdapter - displays either of progress bar, load error, or normal header. Its ViewType is different from the one for MovieAdapter.
MovieAdapter - It extends ListAdapter and displays movies with paging.
FooterAdapter - displays either of a progress bar, load error, or normal footer. Its ViewType is different from the one for MovieAdapter.
They all set up like this
recyclerView.adapter = ConcatAdapter(
ConcatAdapter.Config.Builder().setIsolateViewTypes(false).build(),
headerAdapter,
movieAdapter,
footerAdapter
)
The issue is that the scroll position jumps to the top when users scroll up, reach to the top, load more movies and prepend the newly fetched movies with ListAdapter
's submitList()
But I want to keep the current position not jumps to the top.
Let me break down the problem scenario
I have 50 movies
{100, 101, .... 150}
in the RecyclerView and they are already displayedI scroll all the way up to the top and now see the progress bar that is displayed by the HeaderAdapter
I load 50 more movies
{49, 50, 51, ... 99}
and prepend them to the list as followingsval temp = mutableListOf<Value>() val newMovies = loadMoreMoviesFrom(lastMovieId) temp.addAll(newMovies) // temp = {49, 50, 51, .... 99} temp.addAll(currentList) // temp = {49, 50, 51, .... , 99, 100, 101, ..... 150} submitList(temp)
Then the list does not keep the current position but jumps to the top which shows the progress bar (Header) and 49, 50 as per the gif below. But I want to keep the position as it is.
But if I APPEND new movies in the same way as I did for prepeding, it actaully works as expected
Let's say I have 50 movies {0, 1, 2, ... 49}, scroll all the way down to the bottom, see the progress bar displayed by FooterAdapter, load more 50 movies {50, 51, .... 100}, append them to the list and submit. Then the list keeps the current position as per the gif below
I know I can use Android paging library 3 but I do have some reason why I want to paginate on my own and I don't want to explain it here because it's not the question.
The method to load another 50 chunks, prepend and submit is called from RecyclerView.OnScrollListener() when the scroll position reaches the end. I just took the part only and put it here for easy understanding.
So why the list jumps to the top when I prepend but stays at the current position when I append. How can I prevent the list from jumping to the top when I prepend?
Thank you guys and hope the gifs are of any help