Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override methods notifyDataSetChanged, notifyItemChanged ... of RecyclerView Adapter

As the title. I'm writing a custom RecyclerView which supports multi select mode. And I need tracking selected/unselected state of each item. So after data size of recyclerView has changed. I want to update size of my tracking state list. But I don't know where to override methods : notifyDataSetChanged, notifyItemChagned ....

like image 385
Hoang Ha Avatar asked Dec 18 '15 07:12

Hoang Ha


3 Answers

As the previous answer already correctly stated. You can't as those methods are final.

I came into the same situation when implementing the FastAdapter

The only solution I came up with is to name those methods slightly different. notifyDataSetChanged -> notifyAdapterDataSetChanged https://github.com/mikepenz/FastAdapter/blob/develop/library/src/main/java/com/mikepenz/fastadapter/FastAdapter.java#L1354

public void notifyAdapterDataSetChanged() {
    //... your custom logic
    notifyDataSetChanged();
}

For the library it was quite important to improve the documentation regarding this point, but it is the only solution as of now.

like image 125
mikepenz Avatar answered Oct 27 '22 09:10

mikepenz


You can't because it's final in RecyclerView.Adapter see here

You can override using BaseAdapter with ListView

@Override
public void notifyDataSetChanged() {
    // TODO Auto-generated method stub
    super.notifyDataSetChanged();
}
like image 26
Kishore Jethava Avatar answered Oct 27 '22 08:10

Kishore Jethava


Register the adapter as the RecyclerView.Adapter's observer.

yourRecyclerView.getAdapter().registerAdapterDataObserver(new AdapterDataObserver() {
    @Override
    public void onChanged() {
        // Do nothing
    }
});
like image 45
George Arokiam Avatar answered Oct 27 '22 10:10

George Arokiam