I wanted to animate a row when using realm and RecyclerView on Android, but I gave up

environment

Introduction

Android is almost a beginner. So be gentle .. It's hard to get rid of Recycler as Recycle ..

What did you do

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.

Introduction?

I felt that the following method is a straightforward method to animate add, update, move, and delete to Row of RecyclerView on Android.

notifyItemChanged / notifyItemRangeChanged
notifyItemInserted / notifyItemRangeInserted
notifyItemMoved
notifyItemRemoved / notifyItemRangeRemoved

Subclass of ItemAnimator

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.

Summary so far

If you want to animate the Row in RecyclerView, you should call the notifyItemXXX type method after deleting the dataset.

Realm here

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

I wanted to animate a row when using realm and RecyclerView on Android, but I gave up
Introducing what I made when I wanted to add a header and footer to RecyclerView
[Android] I quit SQLite and tried using Realm
I want to issue a connection when a database is created using Spring and MyBatis
I tried adding a separator line to TabLayout on Android
A note when I was addicted to converting Ubuntu on WSL1 to WSL2
I want to download a file on the Internet using Ruby and save it locally (with caution)
I tried to make a simple face recognition Android application using OpenCV
I summarized the points to note when using resources and resources in combination
[Android] How to turn the Notification panel on and off using StatusBarManager
A story I was addicted to when testing the API using MockMVC
[Android] How to pass images and receive callbacks when sharing using ShareCompat
I made a calculator app on Android
A note that I gave up trying to make a custom annotation for Lombok
A story when I tried to make a video by linking Processing and Resolume
What I tried when I wanted to get all the fields of a bean
I tried using "nifty cloud mobile backend" and "Firebase" Authentication on Kotlin + Android