Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to registerDataSetObserver() in RecyclerView

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?

like image 222
Diffy Avatar asked Nov 13 '14 10:11

Diffy


People also ask

What does notifyDataSetChanged do in RecyclerView?

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.

What is RecyclerView getItemCount?

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.

What are differences between RecyclerView adapter and Listadapter?

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().

What is RecyclerView ViewHolder?

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.


1 Answers

Use registerAdapterDataObserver() instead.

Sample usage:

mRecyclerViewAdapter.registerAdapterDataObserver(myObserver);
like image 152
Pedro Oliveira Avatar answered Oct 10 '22 08:10

Pedro Oliveira