Ayech Hamza
Ayech Hamza

Reputation: 159

Detect if the recyclerview is updated

I have a RecyclerView that contains a list of messages. I want to make a notification every time the user receive a message. Because an update on the RecyclerView means that there is a new message. I tried to make notifications when listening to the Firebase database messages node but it gives me a lot of notifications and make the app looks messy. Any suggestion?

Upvotes: 3

Views: 3054

Answers (2)

Atef Hares
Atef Hares

Reputation: 4881

You can use the RecyclerView.AdapterDataObserver to observe your adapter's data.

it gives you a lot of options to to observe your adapter's data

  • void onChanged()
  • onItemRangeChanged(int positionStart, int itemCount, Object payload)
  • onItemRangeChanged(int positionStart,int itemCount)
  • onItemRangeInserted(int positionStart, int itemCount)
  • onItemRangeMoved(int fromPosition, int toPosition,int itemCount)
  • onItemRangeRemoved(int positionStart, int itemCount)

kotlin example:

mAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver(){

 override fun onChanged() {
   // do work here
    super.onChanged()
 }

})

Upvotes: 10

Ulysses Caetano Braga
Ulysses Caetano Braga

Reputation: 138

After updating your adapter instance, call your_adapter.notifyDataSetChanged();

More details here.

Upvotes: 0

Related Questions