[英]How to use notifyDataSetChanged() in recyclerview, when I have two data sources for the recyclerview, but only one data source has been changed
Currently, I have two Lists
holding data of different types. 目前,我有两个包含不同类型数据的
Lists
。 I pass the two lists to the recyclerview's constructor. 我将两个列表传递给recyclerview的构造函数。 But the constructor doesn't get called on
notifyDataSetChanged()
, so how would I repopulate the Lists
I create in the adapter's constructor, using the parameters? 但是构造函数不会在
notifyDataSetChanged()
上被调用,那么如何使用参数重新填充在适配器的构造函数中创建的Lists
? Also, how do I re-intialize the count variable so my little hack works? 另外,如何重新初始化count变量,以使我的小技巧起作用?
Here's my code - 这是我的代码-
public class SomeAdapter extends RecyclerView.Adapter<PrefAdapter.PrefViewHolder> {
LayoutInflater inflater;
List<Class1> SomeList1;
List<Class2> SomeList2;
int count = 0;
public PrefAdapter(Context context, List<Class1> SomeList1, List<Class2> SomeList2) {
inflater = LayoutInflater.from(context);
this.SomeList1 = SomeList1;
this.SomeList2 = SomeList2;
}
@Override
public PrefViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.row, parent, false);
return new SomeViewHolder(view);
}
@Override
public void onBindViewHolder(SomeViewHolder holder, int position) {
if(count >= SomeList2.size()) {
if (nameAndNumberList.size() > 0) {
holder.name.setText(SomeList1.get(count-SomeList2.size()).getName());
holder.number.setText(SomeList1.get(count-SomeList2.size()).getNumber());
count+=1;
}
}else {
holder.name.setText(SomeList2.get(position).getName());
holder.number.setText(SomeList2.get(position).getNumber());
count+=1;
}
}
@Override
public int getItemCount() {
return SomeList1.size()+SomeList12.size();
}
Update - 更新-
Turns out I was being really stupid. 原来我真的很傻。 Just removed the count variable.
刚刚删除了count变量。
position
does the job of count. position
发挥了重要作用。 I need to get some sleep. 我要睡觉了
The best idea is repopulate the data in the existing List
. 最好的办法是重新填充现有
List
的数据。 For example: 例如:
SomeList1.clear();
SomeList1.addAll(list2);
Now invoke the notifyDataSetChanged()
, so the data in the recyclerview's is updated 现在调用
notifyDataSetChanged()
,以便更新recyclerview中的数据
adapter.notifyDataSetChanged()
In your constructor, you're setting this.SomeList1
to the SomeList1
that was passed in. This makes it so that this.SomeList1
points to the same list instance as the code that passed it to the constructor. 在构造函数中,您将
this.SomeList1
设置为传入的SomeList1
。这使得this.SomeList1
指向与将其传递给构造函数的代码相同的列表实例。
Since they're pointing to the same list instances, you can update them as you see fit in the calling code, and the adapter will be none the wiser. 由于它们指向相同的列表实例,因此您可以根据需要在调用代码中对其进行更新,而适配器将是最明智的选择。 Calling
notifyDataSetChanged
after changes were made will cause the adapter to refresh itself. 进行更改后调用
notifyDataSetChanged
将导致适配器刷新。 This causes getItemCount
to be re-evaluated. 这将导致对
getItemCount
进行重新评估。 Since the lists are now updated, you'll get the updated count. 由于列表现在已更新,因此您将获得更新的计数。
// Initialize adapter. Empty lists.
List<Class1> list1 = new ArrayList<>();
List<Class2> list2 = new ArrayList<>();
PrefAdapter adapter = new PrefAdapter(context, list1, list2);
// Set adapter on recycler view.
...
// Update lists. First list has 1 item, second list has 2.
list1.add(new Class1());
list2.add(new Class2());
list2.add(new Class2());
// Notify adapter that data has changed.
// This causes the adapter to refresh itself.
// Since the list instances are the same,
// when getItemCount is re-evaluated, it will return 3.
adapter.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.