简体   繁体   English

使用 RecyclerView 时,notifydatasetchanged 是否调用 onCreateViewHolder

[英]Does notifydatasetchanged call onCreateViewHolder when using RecyclerView

I want to use a toggle to toggle between two different views but using the same RecyclerView .我想使用切换在两个不同的视图之间切换,但使用相同的RecyclerView Basically, once you toggle, I want the RecyclerView adapter to recall onCreateViewHolder() but this time it will use a different layout item file.基本上,一旦您切换,我希望RecyclerView适配器调用onCreateViewHolder()但这次它将使用不同的布局项文件。

Does notifydatasetchanged() cause the adapter to rebuild itself? notifydatasetchanged()是否会导致适配器自行重建? 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.我的RecyclerView Adapter上的View也需要有两种类型,一种用于“常规”模式,另一种用于多选模式。

So, you can override getItemViewType to force the Adapter to call your onCreateViewHolder for all views.因此,您可以覆盖getItemViewType以强制适配器为所有视图调用您的onCreateViewHolder

Add this to the Adapter code:将此添加到Adapter代码:

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 :将此添加到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.由于您在ActionMode返回不同的ViewType ,因此 Adapter 被迫丢弃所有创建的视图,并重新创建所有内容。

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.如果你想刷新 RecyclerView 项目和 onCreateView() 也被调用,比如网格和列表。 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要删除和更新RecyclerView布局,您可以调用

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.我在这个问题上花了 6 多个小时没有任何成功。 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()然后覆盖 getItemType()

  @Override
    public int getItemViewType(int position) {

       return mCurrentViewType;


    }

my toggleItemViewType method:我的 toggleItemViewType 方法:

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!我正在访问来自不同类的变量,这是不对的,但是现在,为了 onCreateViewHolder 问题,它起作用了! if you have a better solution then good luck and share it with us.如果您有更好的解决方案,那么祝您好运并与我们分享。 don't forget to make the global variable as "static" :)不要忘记将全局变量设为“静态”:)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 离线时Recyclerview不会调用onCreateViewHolder - Recyclerview not call onCreateViewHolder when offline 使用 ViewModelProviders 时 RecyclerView.Adapter 未达到 onCreateViewHolder? - RecyclerView.Adapter does not reach onCreateViewHolder when using ViewModelProviders? recyclerview 适配器如何知道调用 onCreateViewHolder? - How does the recyclerview adapter know to call onCreateViewHolder? 强制RecyclerView调用onCreateViewHolder - Force RecyclerView to call onCreateViewHolder Recyclerview 不调用 onCreateViewHolder - Recyclerview not call onCreateViewHolder 不是抽象的,并且不会在Android中使用recyclerView覆盖onCreateViewHolder的抽象方法 - Not abstract and does not override abstract method onCreateViewHolder using recyclerView in android 调用notifyDataSetChanged()时,RecyclerView不会更新或刷新 - RecyclerView does not update or refresh when calling notifyDataSetChanged() 什么时候调用onCreateViewHolder,为什么? 回收站视图 - When is onCreateViewHolder called and why? RecyclerView Recyclerview 不调用任何 Adapter 方法:onCreateViewHolder、onBindViewHolder - Recyclerview not call any Adapter method: onCreateViewHolder, onBindViewHolder RecyclerView不会调用onCreateViewHolder和其他方法 - RecyclerView doesn't call onCreateViewHolder and other methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM