In listView, to listen to changes in the Adapter data set, i use
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
}
});
But i am uanble to find a function that does the similar job in a RecyclerView. How can it be achieved in case of RecyclerView.Adapter?
notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.
getItemCount() - returns The number of items currently available in adapter. This method returns the size of the collection that contains the items we want to display.
The RecyclerView's adaptor forces us to use the ViewHolder pattern. The views are split into onCreateViewholder() and onBindViewholder() methods. The ListView doesn't give that kind of protection by default, so without implementing the ViewHolder pattern inside the getView().
A RecyclerView. ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results.
Use registerAdapterDataObserver()
instead.
Sample usage:
mRecyclerViewAdapter.registerAdapterDataObserver(myObserver);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With