Sange
A lightweight library that quickly implements RecyclerView paging loading.
Item introduction:
Sange is an extremely accurate weapon. It has incredible spirituality, as if it would find its own weaknesses to attack.
Increase the power of 16.
Increase the attack power by 10.
Disabled (passive): There is a 15% chance that the target will be disabled in the attack. The disability effect reduces the target's movement speed by 20% for 4 sec.
Prepare
- Add jitpack to build.gradle
- Add dependency
Start
First Blood
-
The function of the Sange core is DataSource, which makes it easy to initialize and page load data in a few simple steps.
Before that, we have to define our data types first, remember to implement the SangeItem interface. For example:
-
Next create your own DataSource.
As shown below, we inherit MultiDataSource and treat SangeItem as a generic parameter, then implement the loadInitial and loadAfter methods:
Both the loadInitial and loadAfter methods will be called in the child thread, so there is no need to worry about any time-consuming operations in both methods.
After the data is loaded, just call LoadCallback's setResult(list) method, and Sange will do everything else for you.
Including thread switching, notification interface updates, etc., you need to do, just focus on the loading of data. -
Next, create a ViewHolder for display. By inheriting the SangeViewHolder provided by the Sange, you can omit many other tedious tasks.
E.g:
-
The next step is to create your own Adapter. By inheriting the SangeMultiAdapter provided by Sange, you can easily combine the DataSources.
E.g:
-
Finally, associate the RecyclerView with the Adapter:
That's it, you don't need to care about the logic of paging, you just need to focus on what you really should pay attention to: Load the data, and leave it to the Sange!
Double Kill
So far we have all gone well, but it seems that we lack the status display of page load. Let's implement it.
-
In order to display the loaded state, we first create a data type that represents the state:
As you can see, we also implemented the SangeItem interface and implemented the viewType method, which returns a new Type type in the method.
-
Then we slightly modify the DataSource, we implement an additional method: onStateChanged(newState).
This method will be called at different stages of the page load to tell us the current state of the DataSource, such as loading, loading failure, loading success, etc.
By implementing this method, we can control the display of the loading state and customize the style of the display.As shown above, we have added a Data Item item to represent the status.
-
Again, we need a ViewHolder that renders the State:
-
Finally, the loading status is displayed.
Triple Kill
-
Refresh and retry
The Sange DataSource provides invalidate() and retry() methods.
When the data needs to be refreshed, the invalidate() method can be called.
When the load fails and needs to be retried, it is called. retry() method. -
Custom DiffCallback
Sange uses DiffUtil to update RecyclerView efficiently.
You can change the comparison logic according to your actual situation: