ValueEventListener vs ChildEventListener for RecyclerView in Android

后端 未结 4 868
暖寄归人
暖寄归人 2020-12-15 18:06

Firebase Database users know that there are two basic listeners for listening Data: ValueEventListener and ChildEventListener. It works great when

4条回答
  •  再見小時候
    2020-12-15 18:37

    I would anyday use onchildAdded listener in this case, there are several advantages of it, firstly you have figured out the network operation that is suppose there are 100 posts and even if one gets changed you will get callback of all of them. Whereas in onChildListener you will get callback of the only post that got changed. So what you can do is map the callbacks of firebase with the recycler views methods like this::-

    onChildAdded --> you must cast the dataSnapshot into your class and call recyclerView.notifyItemAdded()

    onChildRemoved --> recyclerView.notifyItemRemoved(int)

    onChildChanged --> recyclerView.notifyItemChanged(int)

    onChildMoved --> (you probably don't need this, it is for ordering/priority)

提交回复
热议问题