0

Is there a way to get a current number of items in RecyclerView every time when notifyDataSetChanged() method is called? I'd like to use something like LiveData, so i can observe changes in RecyclerView.

2 Answers2

2

You can simply get the size of the list in recyclerView every time you call notifyDataSetChanged() or you can use listeners to get a cleaner code.

danial iranpour
  • 282
  • 1
  • 2
  • 11
  • That's what i want - use a listener. But is seems like RecyclerView doen't have any default listeners to observe the number of items. – Vladyslav Vlad Apr 03 '21 at 12:24
  • No it doesn't have any, but you can add it to your class if you want, visit [this](https://stackoverflow.com/a/18585247/9426046) if you don't know how to add listeners. – danial iranpour Apr 03 '21 at 12:28
0

notifyDataSetChanged() rebinds holders, so you can try to get this data inside of onBindViewHolder() method.

Yegorf
  • 400
  • 2
  • 11
  • putting it inside `onBindViewHolder` doesn't seem useful - at this point you will already have called notifyDataSetChanged, so you could have checked the size _before_ calling it – a_local_nobody Apr 03 '21 at 16:10