简体   繁体   English

检测recyclerview是否更新

[英]Detect if the recyclerview is updated

I have a RecyclerView that contains a list of messages.我有一个包含消息列表的RecyclerView 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.因为RecyclerView的更新意味着有新消息。 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.我试图在收听 Firebase 数据库消息节点时发出通知,但它给了我很多通知并使应用程序看起来很混乱。 Any suggestion?有什么建议吗?

You can use the RecyclerView.AdapterDataObserver to observe your adapter's data.您可以使用RecyclerView.AdapterDataObserver来观察适配器的数据。

it gives you a lot of options to to observe your adapter's data它为您提供了许多观察适配器数据的选项

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

kotlin example:科特林示例:

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

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

})

After updating your adapter instance, call your_adapter.notifyDataSetChanged();更新适配器实例后,调用 your_adapter.notifyDataSetChanged();

More details here .更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM