简体   繁体   English

当我有两个recyclerview数据源,但是只有一个数据源被更改时,如何在recyclerview中使用notifyDataSetChanged()

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

相关问题 在另一个片段中更改数据时如何刷新一个片段中的RecyclerView - How to refresh RecyclerView in one fragment when data changed in another fragment 如何通知 RecyclerView 数据已从适配器内的计时器中更改 - How can I inform the RecyclerView that data has changed from within a Timer inside the Adapter 即使更改了数据集,RecyclerView 也不会引用 - RecyclerView not refershing even when data set is changed 在 Firebase 数据库中添加或更改数据时,如何停止 Recyclerview 中的内容闪烁 - How do I stop blinking of content in Recyclerview when data is added or changed in Firebase database 如何在 RecyclerView 中使用 HashMap 数据? - How to use HashMap data in RecyclerView? 更改微调器项目数据时更新recyclerview单行数据 - Update recyclerview single row data when spinner item data is changed 如何从两个表中获取数据的回收视图? - How do I poblate a recyclerview that takes data from two tables? 如何仅在 recyclerview 中显示具有特定值的数据 - How can I display data in recyclerview only with specific value 如何更新已删除所有数据的recyclerview - How to update the recyclerview that has deleted all data 如何在片段重新创建中从 firebase 恢复已在 recyclerview 中提取的数据 - how to restore the data that has already been fetched in recyclerview from firebase in fragment recreation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM