io.realm:realm-gradle-plugin:2.2.0
org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.6
Android is almost a beginner. So be gentle ..
It's hard to get rid of Recycler
as Recycle
..
When using Realm and RecyclerView, I wanted to run Row Animation when deleting data (updating in model), but I didn't do it because of various reasons.
I felt that the following method is a straightforward method to animate add
, update
, move
, and delete
to Row of RecyclerView on Android.
Create a subclass that inherits ʻandroid.support.v7.widget.RecyclerView.ItemAnimator`
Override the required method
Call the notification method of __ other than notifyDataSetChanged
__ of Adapter according to the operation you want to do (* 1)
Events fall into subclasses of ItemAnimator.
1: RecyclerView data manipulation notification method
notifyItemChanged / notifyItemRangeChanged
notifyItemInserted / notifyItemRangeInserted
notifyItemMoved
notifyItemRemoved / notifyItemRangeRemoved
This time, I decided to use SimpleItemAnimator.
A wrapper class for ItemAnimator that records View bounds and decides whether it should run move, change, add or remove animations. This class also replicates the original ItemAnimator API.
As you can see in the official Document, it seems to be a class that wraps ItemAnimator in an easy-to-use manner, so I thought this would be enough if I didn't do anything elaborate.
Next, I implemented the necessary methods.
class MainTimelineRowAnimator : SimpleItemAnimator() {
override fun runPendingAnimations() {...}
override fun animateAdd(holder: RecyclerView.ViewHolder?): Boolean {...}
override fun animateChange(oldHolder: RecyclerView.ViewHolder?, newHolder: RecyclerView.ViewHolder?, fromLeft: Int, fromTop: Int, toLeft: Int, toTop: Int): Boolean {...}
override fun animateMove(holder: RecyclerView.ViewHolder?, fromX: Int, fromY: Int, toX: Int, toY: Int): Boolean {...}
override fun animateRemove(holder: RecyclerView.ViewHolder?): Boolean {...}
override fun isRunning(): Boolean {...}
override fun endAnimation(item: RecyclerView.ViewHolder?) {...}
override fun endAnimations() {...}
}
This article was very easy to understand for the explanation of ItemAnimator of RecyclerView.
If you want to animate the Row in RecyclerView, you should call the notifyItemXXX
type method after deleting the dataset.
To use realm with RecyclerView, we use RealmRecyclerViewAdapter
. Let's take a look at the implementation here.
// Right now don't use generics, since we need maintain two different
// types of listeners until RealmList is properly supported.
// See https://github.com/realm/realm-java/issues/989
this.listener = hasAutoUpdates ? new RealmChangeListener() {
@Override
public void onChange(Object results) {
notifyDataSetChanged();
}
} : null;
As discussed in Github, it says that it does not yet support detailed notifications. So even if you call notifyItemRemoved () on the Activity, Fragment side, it doesn't respond ...
Recommended Posts