[英]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.