简体   繁体   English

Android-RecyclerView notifyDatasetChanged不更新布局视图

[英]Android - RecyclerView notifyDatasetChanged not updating layout view

I have an Adapter for a RecyclerView . 我有一个用于RecyclerView的适配器。 The adapter has a property size, that can be either SMALL or LARGE. 适配器的属性大小可以是SMALL或LARGE。

Depending on the property, the layouts loaded for viewholders will be different. 根据属性的不同,为视口加载的布局将有所不同。

I have a button to change this property, however after I set it, and if I call notifyDataSetChanged() the views are not updated. 我有一个更改此属性的按钮,但是在设置它后,如果我调用notifyDataSetChanged()则视图不会更新。

I have also tried requestLayout and invalidate without success. 我也尝试了requestLayout并使invalidate失败。 This is, onCreateViewHolder is never called again. onCreateViewHolder ,永远不会再次调用onCreateViewHolder

notifyDataSetChanged() call onBindViewHolder() when using RecyclerView.It doesnot call onCreateViewHolder. notifyDataSetChanged()在使用RecyclerView时调用onBindViewHolder()。它不会调用onCreateViewHolder。 You can try setAdapter() for the above problem. 您可以尝试使用setAdapter()解决上述问题。

One way to do is like calling 一种方法是像打电话

recyclerView.setAdapater(new YourCustomRecylcerViewAdapter(yourProperty, listToUpdate))

whenever you change the property. 每当您更改属性时。 This will invoke the onCreateViewHolder() of adapter. 这将调用适配器的onCreateViewHolder()

Well I got it, it was because before onCreateViewHolder , getItemViewType is called. 我知道了,这是因为在onCreateViewHolder之前, getItemViewType

If no new view is set then it won't be called. 如果未设置新视图,则不会调用它。 So I had to create a new viewType for each of the small views and problem solved :). 所以我不得不为每个小的视图创建一个新的viewType并解决问题:)。

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

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