Android在RecyclerView中添加/替换项目 [英] Android add/replace Items within RecyclerView

查看:235
本文介绍了Android在RecyclerView中添加/替换项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关此主题的话题已经很多,但是到目前为止,给定的解决方案都没有对我有用.我正在尝试添加或更新RecyclerView的项目.到目前为止,这是我的代码:

I know there are lots of threads already on this topic, but none of the given solutions worked for me so far. I'm trying to add or update an item of a RecyclerView. Here's my code so far:

MainActivity

private MyListItemAdapter mAdapter;
private RecyclerView recyclerView;

// called on activity create
private void init() {
    // initialize activity, load items, etc ...
    mAdapter = new MyListItemAdapter(this, items);
    recyclerView.setAdapter(mAdapter);
}

// called when I want to replace an item
private void updateItem(final Item newItem, final int pos) {
    mAdapter.replaceItem(newItem, pos);
}

MyListItemAdapter

public class MyListItemAdapter extends RecyclerView.Adapter<MyListItemAdapter.MyListItemViewHolder> {

    private List<Item> mItems;

    public void replaceItem(final Item newItem, final int pos) {
        mItems.remove(position);
        mItems.add(position, newItem);

        notifyItemChanged(position);
        notifyDataSetChanged();
    }    
}

我也尝试从MainActivity进行此更改,但是在每种情况下,我尝试的列表都不会更新.它的唯一工作方式是当我将适配器重置为recyclerView时:

I tried to make this changes from the MainActivity aswell, but in every case I tried my list doesn't get updated. The only way it worked was when I reset the adapter to the recyclerView:

mAdapter.notifyDataSetChanged();
recyclerView.setAdapter(mAdapter);

这显然不是一个好主意. (除了不良的副作用,当我在列表上使用延迟加载时甚至无法使用).

which obviously is a bad idea. (aside from the bad side effects wouldn't even work when I'm using lazy loading on my lists).

所以我的问题是,如何使notifyDataSetChanged()正常工作?

So my question is, how can I make notifyDataSetChanged() work properly?

修改

我找到了替换物品的解决方案.在mAdapter.replaceItem(newItem, pos);之后,我不得不打电话给recyclerView.removeViewAt(position);

I found a solution for replacing items. After mAdapter.replaceItem(newItem, pos); I had to call recyclerView.removeViewAt(position);

这可以替换项目,但是当我想将项目(例如延迟加载)添加到列表中时并不能解决我的问题

This works for replacing an item, but doesn't solve my problem when I want to add items (e.g. lazy loading) to my list

edit2

我找到了添加项目的可行解决方案

I found a working solution for adding items

适配器:

public void addItem(final Item newItem) {
    mItems.add(newItem);
    notifyDataSetChanged();
}

活动:

private void addItem(final Item newItem) {
    mAdapter.addItem(newItem);
    recyclerView.removeViewAt(0); // without this line nothing happens
}

由于某种原因,它可以工作(也:它不会删除位置0的视图),但是我确定这不是将项目添加到recyclerView的正确方法

For some reason this works (also: it doesn't remove the view at position 0), but I'm sure this isn't the correct way to add items to a recyclerView

推荐答案

这应该有效:

private ArrayList<Item> mItems;

public void replaceItem(final Item newItem, final int position) {
    mItems.set(position, newItem);
    notifyItemChanged(position);
}  

ArrayList.set()是替换项目的方法.

要添加项目,只需将它们附加到mItems,然后转到notifyDatasetChanged().另一种方法是使用notifyItemRangeInserted().根据您在何处/如何添加新项目以及其中有多少新项目,这可能是值得的.

For adding items, just append them to mItems and then go notifyDatasetChanged(). Another way to go is to use notifyItemRangeInserted(). Depending on where/how are you adding new items and how many of them, it might be worth it.

这篇关于Android在RecyclerView中添加/替换项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆