Android SortedList Explained
If you want to be good android developer, you shouldn’t call notifyDataSetChanged() whenever your list is changed. It is really costly. I am not going to talk about how costly it is. You can find dozens of document how to notify your recyclerview adapter in a performant way. In this article, I am going to talk about more interesting thing: SortedList.
I know you know how to sort a list. But now I am telling you that you don’t need to sort your list to keep it in order. Yes you heard that true :)
There is more. Let say your application is built on MVP pattern. You have a recyclerview which is showing a list of products. You do some business on your presenter and you want to notify your view about list is changed. You have to choose proper method for your business operation. You may want to add an item to specific index. You may want to update specific item. Or you may want to remove 3 of them. You can not update your all list by calling notifyDataSetChanged(). So you need to calculate your old list and new list difference.
In this case DiffUtill comes to our help. Wait. But I am not going to solve that problem with DiffUtil too. I don’t want to calculate diff only, also I want to have more control over add, remove, update on my recyclerview adapter. If you want to know about DiffUtil you can just go through the link below and read my blogpost about it.
Lets Explore the Toys?
SortedList
A Sorted list implementation that can keep items in order and also notify for changes in the list such that it can be bound to a RecyclerView.Adapter
.
It keeps items ordered using the compare(Object, Object)
method and uses binary search to retrieve items.
SortedList.CallBack
It defines how items should be sorted and how duplicates should be handled. SortedList takes this as a callback and notify this callback’s methods.
SortedList.BatchedCallback
This class can be useful if you want to do multiple operations on a SortedList but don’t want to dispatch each event one by one, which may result in a performance issue. If you add items to list in a loop which is iterated 100 times, you don’t want to get notified 100 times onInserted(), do you?
SortedListAdapterCallback
Actually this is the SortedList.CallBack implementation which takes you adapter as a parameter and also overrides onInserted(), onRemoved(), onMoved(), onChanged() methods from its ancestor.
Show me the code!
I created a demo app for sorted list. It has list of users. You can do following in the sample project;
At all of below, you don’t care about order and you don’t need to notify adapter about the change. SortedList takes care about it. List is kept itself in order automagically.
- Add random user to list.
- Add bulk user (10 users) to list.
- Remove random user
- Change list order criteria (Sort by user age)
- Change list order criteria (Sort by user score)
I put little more abstraction in sample source.
But I want to give as simple as possible in this blogpost. If you interested in with SortedList you can go through my sample source code.
Create your POJO
Create your Adapter
That is all.
In this adapter, we sorted our list by user’s score. That is all. All we have to do is switch our list to SortedList in our adapter and override 3 adapter callback methods: compare(), areContentsSame() and areItemsSame(). As I said before this is the simplest way to implement sortedList to your existing adapter. And did you notice we put our sorting criteria into adapter? It always sorts our list by user score. I got this comparator out of there in sample project. I am not posting those codes here to keep this article clean and basic.
Note: I am so new in Kotlin. Also, I am still learning how to use SortedList effectively. Please don’t hesitate to comment and give feedback. Thanks for reading. You can clap to spread more :)
Happy coding.