I have a RecyclerView
that loads messages as a regular messenger - after scrolling too close to top. I need to put there a circle loading indicator
as the loading continues and remove it after it is finished and add loaded messages.
How do I create and delete such indicator?
CodePudding user response:
If I got you right, then besides view holders with message itself, you also need a view holder with a progress bar in it, which appears and disappears. You can go with different approaches: a) use Android paging library, b) use third-party libraries like FastAdapter, Epoxy and so on, c) write your own code
First two options already support mentioned in your question functionality, but I will be focused on the third option in this answer. You'll have to add:
- a scroll listener which will detect when you're almost at the top/bottom of the list take a look at this example
- adapter which works with two types of view holders
- adapter which supports DiffUtil - you can use native Android ListAdapter (we don't want to have a lagging view)
Let's say you have a bunch of data - mutableListOf(messages)
. Before getting a new list of messages from your server, you can add to this list a new message model (with empty fields or you could add some kind of optional field in your Message data class like viewItem = MessageItem
or viewItem = ProgressItem
to use it later in getItemViewType()
) and then send this list (your list of models progress model at the end of the list) to adapter. When you get a response from your server, you can just set the whole new list to adapter - DiffUtil will take care of all needed changes and will remove the item with the progress bar.
Happy coding!