简体   繁体   中英

Does notifydatasetchanged call onCreateViewHolder when using RecyclerView

I want to use a toggle to toggle between two different views but using the same RecyclerView . Basically, once you toggle, I want the RecyclerView adapter to recall onCreateViewHolder() but this time it will use a different layout item file.

Does notifydatasetchanged() cause the adapter to rebuild itself? Or is there another way?

I needed to have two types on View s on my RecyclerView Adapter as well, one for 'regular' mode and one for multi-select mode.

So, you can override getItemViewType to force the Adapter to call your onCreateViewHolder for all views.

Add this to the Adapter code:

public void setActionMode(ActionMode actionMode) {
    this.actionMode = actionMode;
    notifyDataSetChanged();
}

@Override
public int getItemViewType(int position) {
    return (actionMode == null ? 0 : 1);
}

Add this to the ViewHolder :

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == 0) {
        view = inflater.inflate(R.layout.layout_1, parent, false);
    } else {
        view = inflater.inflate(R.layout.layout_2, parent, false);
    }
    ...
}

Since you return a different ViewType when in an ActionMode , the Adapter is forced to throw away all created views, and recreate everything again.

notifyDataSetChanged()RecyclerView情况下调用onBindViewHolder()

THE BEST/SIMPLEST SOLUTION

If you want to refresh RecyclerView items and onCreateView() be called too, say for a Grid and a List. here is how to do that. Kotlin

fun refreshRecyclerView(recyclerView:RecyclerView){
     val adapterRef=recyclerView.adapter
     recyclerView.adapter=null
     recyclerView.adapter=adapterRef
     }

Java

void refreshRecyclerView(RecyclerView recyclerView){
        Adapter adapterRef=recyclerView.getAdapter();
        recyclerView.setAdapter(null);
        recyclerView.setAdapter(adapterRef);
        }

To remove and update layout in RecyclerView , you can call

mRecyclerView.removeView(view);

OR

mRecyclerView.removeViewAt(position);

after removing object in your dataset

I spent more than 6 hours on this issue without any success. Finally!!! I set a global variable in the adapter and had to set it up every time i toggled the view from list to grid (in my case). the funny thing this approauch was there but I forgot to do it as static!! So my solution could be related to yours , just try it and hope it works out.

public static int mCurrentViewType;

then override the getItemType()

  @Override
    public int getItemViewType(int position) {

       return mCurrentViewType;


    }

my toggleItemViewType method:

public void toggleItemViewType () {
        if (mCurrentViewType == LIST_ITEM){
            mCurrentViewType = GRID_ITEM;
        } else {
            mCurrentViewType = LIST_ITEM;
        }
    }

I am accessing the variable from different classes, which is not right, but for now and for the sake of the onCreateViewHolder issue, it worked! if you have a better solution then good luck and share it with us. don't forget to make the global variable as "static" :)

是的,它会假设其当前数据集无效,并且需要重新布局和重新绑定所有布局。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM