繁体   English   中英

如何更新 RecyclerView 适配器数据

[英]How to update RecyclerView Adapter Data

我想弄清楚更新RecyclerView的适配器有什么问题。

在获得新的产品列表后,我尝试:

  1. 从创建recyclerView的fragment更新ArrayList ,设置新数据到adapter,然后调用adapter.notifyDataSetChanged() 这没用。

  2. 像其他人一样创建一个新的适配器,它对他们有用,但对我来说没有任何变化: recyclerView.setAdapter(new RecyclerViewAdapter(newArrayList))

  3. Adapter创建一个更新数据的方法,如下所示:

     public void updateData(ArrayList<ViewModel> viewModels) { items.clear(); items.addAll(viewModels); notifyDataSetChanged(); }

    然后每当我想更新数据列表时我都会调用这个方法; 这没用。

  4. 为了检查我是否可以以任何方式修改 recyclerView,我尝试删除至少一个项目:

     public void removeItem(int position) { items.remove(position); notifyItemRemoved(position); }

一切都保持原样。

这是我的适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

    private ArrayList<ViewModel> items;
    private OnItemClickListener onItemClickListener;

    public RecyclerViewAdapter(ArrayList<ViewModel> items) {
        this.items = items;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    public void updateData(ArrayList<ViewModel> viewModels) {
        items.clear();
        items.addAll(viewModels);
        notifyDataSetChanged();
    }
    public void addItem(int position, ViewModel viewModel) {
        items.add(position, viewModel);
        notifyItemInserted(position);
    }

    public void removeItem(int position) {
        items.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewModel item = items.get(position);
        holder.title.setText(item.getTitle());
        Picasso.with(holder.image.getContext()).load(item.getImage()).into(holder.image);
        holder.price.setText(item.getPrice());
        holder.credit.setText(item.getCredit());
        holder.description.setText(item.getDescription());

        holder.itemView.setTag(item);
    }


    @Override
    public int getItemCount() {
        return items.size();
    }


    @Override
    public void onClick(final View v) {
        // Give some time to the ripple to finish the effect
        if (onItemClickListener != null) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    onItemClickListener.onItemClick(v, (ViewModel) v.getTag());
                }
            }, 0);
        }
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public TextView price, credit, title, description;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.image);
            price = (TextView) itemView.findViewById(R.id.price);
            credit = (TextView) itemView.findViewById(R.id.credit);
            title = (TextView) itemView.findViewById(R.id.title);
            description = (TextView) itemView.findViewById(R.id.description);
        }
    }

    public interface OnItemClickListener {

        void onItemClick(View view, ViewModel viewModel);

    }
}

我启动RecyclerView如下:

recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 5));
adapter = new RecyclerViewAdapter(items);
adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);

那么,我如何实际更新适配器数据以显示新收到的项目?


问题是 gridView 的布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:tag="catalog_fragment"
    android:layout_height="match_parent">

    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"/>

        <ImageButton
            android:id="@+id/fab"
            android:layout_gravity="top|end"
            style="@style/FabStyle"/>

    </FrameLayout>
</LinearLayout>

然后我删除了LinearLayout并将FrameLayout作为父布局。

这是一个普遍的答案。 解释了更新适配器数据的各种方法。 该过程每次包括两个主要步骤:

  1. 更新数据集
  2. 通知适配器更改

插入单项

在索引2处添加“猪”。

插入单项
String item = "Pig";
int insertIndex = 2;
data.add(insertIndex, item);
adapter.notifyItemInserted(insertIndex);

插入多个项目

在索引2处插入另外三只动物。

插入多个项目
ArrayList<String> items = new ArrayList<>();
items.add("Pig");
items.add("Chicken");
items.add("Dog");
int insertIndex = 2;
data.addAll(insertIndex, items);
adapter.notifyItemRangeInserted(insertIndex, items.size());

删除单个项目

从列表中删除“猪”。

删除单个项目
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);

删除多个项目

从列表中删除“骆驼”和“羊”。

删除多个项目
int startIndex = 2; // inclusive
int endIndex = 4;   // exclusive
int count = endIndex - startIndex; // 2 items will be removed
data.subList(startIndex, endIndex).clear();
adapter.notifyItemRangeRemoved(startIndex, count);

删除所有项目

清除整个列表。

删除所有项目
data.clear();
adapter.notifyDataSetChanged();

用新列表替换旧列表

清除旧列表,然后添加新列表。

用新列表替换旧列表
// clear old list
data.clear();

// add new list
ArrayList<String> newList = new ArrayList<>();
newList.add("Lion");
newList.add("Wolf");
newList.add("Bear");
data.addAll(newList);

// notify adapter
adapter.notifyDataSetChanged();

adapter有一个对data的引用,所以重要的是我没有将data设置为一个新对象。 相反,我从data清除了旧项目,然后添加了新项目。

更新单项

更改“羊”项目,使其显示“我喜欢羊”。

更新单项
String newValue = "I like sheep.";
int updateIndex = 3;
data.set(updateIndex, newValue);
adapter.notifyItemChanged(updateIndex);

移动单个项目

将“羊”从位置3移动到位置1

移动单个项目
int fromPosition = 3;
int toPosition = 1;

// update data array
String item = data.get(fromPosition);
data.remove(fromPosition);
data.add(toPosition, item);

// notify adapter
adapter.notifyItemMoved(fromPosition, toPosition);

代码

这是项目代码供您参考。 RecyclerView Adapter 代码可以在这个答案中找到。

主活动.java

public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {

    List<String> data;
    MyRecyclerViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // data to populate the RecyclerView with
        data = new ArrayList<>();
        data.add("Horse");
        data.add("Cow");
        data.add("Camel");
        data.add("Sheep");
        data.add("Goat");

        // set up the RecyclerView
        RecyclerView recyclerView = findViewById(R.id.rvAnimals);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                layoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
        adapter = new MyRecyclerViewAdapter(this, data);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
    }

    public void onButtonClick(View view) {
        insertSingleItem();
    }

    private void insertSingleItem() {
        String item = "Pig";
        int insertIndex = 2;
        data.add(insertIndex, item);
        adapter.notifyItemInserted(insertIndex);
    }

    private void insertMultipleItems() {
        ArrayList<String> items = new ArrayList<>();
        items.add("Pig");
        items.add("Chicken");
        items.add("Dog");
        int insertIndex = 2;
        data.addAll(insertIndex, items);
        adapter.notifyItemRangeInserted(insertIndex, items.size());
    }

    private void removeSingleItem() {
        int removeIndex = 2;
        data.remove(removeIndex);
        adapter.notifyItemRemoved(removeIndex);
    }

    private void removeMultipleItems() {
        int startIndex = 2; // inclusive
        int endIndex = 4;   // exclusive
        int count = endIndex - startIndex; // 2 items will be removed
        data.subList(startIndex, endIndex).clear();
        adapter.notifyItemRangeRemoved(startIndex, count);
    }

    private void removeAllItems() {
        data.clear();
        adapter.notifyDataSetChanged();
    }

    private void replaceOldListWithNewList() {
        // clear old list
        data.clear();

        // add new list
        ArrayList<String> newList = new ArrayList<>();
        newList.add("Lion");
        newList.add("Wolf");
        newList.add("Bear");
        data.addAll(newList);

        // notify adapter
        adapter.notifyDataSetChanged();
    }

    private void updateSingleItem() {
        String newValue = "I like sheep.";
        int updateIndex = 3;
        data.set(updateIndex, newValue);
        adapter.notifyItemChanged(updateIndex);
    }

    private void moveSingleItem() {
        int fromPosition = 3;
        int toPosition = 1;

        // update data array
        String item = data.get(fromPosition);
        data.remove(fromPosition);
        data.add(toPosition, item);

        // notify adapter
        adapter.notifyItemMoved(fromPosition, toPosition);
    }
}

笔记

  • 如果您使用notifyDataSetChanged() ,则不会执行任何动画。 这也可能是一项昂贵的操作,因此如果您仅更新单个项目或一系列项目,则不建议使用notifyDataSetChanged()
  • 如果您要对列表进行大量或复杂的更改,请查看DiffUtil

进一步研究

我正在使用 RecyclerView,删除和更新都运行良好。

  1. 删除

    从 RecyclerView 中删除项目有四个步骤

     list.remove(position); recycler.removeViewAt(position); mAdapter.notifyItemRemoved(position); mAdapter.notifyItemRangeChanged(position, list.size());

    这些代码行对我有用。

  2. 更新数据

    我唯一要做的就是:

     mAdapter.notifyDataSetChanged();

您必须在 Activity/Fragment 代码中完成所有这些操作,而不是在 RecyclerView Adapter 代码中。

这对我有用:

recyclerView.setAdapter(new RecyclerViewAdapter(newList));
recyclerView.invalidate();

在创建一个包含更新列表的新适配器(在我的情况下,它是一个转换为 ArrayList 的数据库)并将其设置为适配器后,我尝试了recyclerView.invalidate()并且它起作用了。

您有两种选择来执行此操作:

从适配器刷新 UI:

mAdapter.notifyDataSetChanged();

或者从 recyclerView 本身刷新它:

recyclerView.invalidate();

另一种选择是使用diffutil 它将原始列表与新列表进行比较,并在发生更改时使用新列表作为更新。

基本上,我们可以使用 DiffUtil 来比较旧数据与新数据,并让它代表您调用 notifyItemRangeRemoved、notifyItemRangeChanged 和 notifyItemRangeInserted。

使用 diffUtil 而不是 notifyDataSetChanged 的​​快速示例:

DiffResult diffResult = DiffUtil
                .calculateDiff(new MyDiffUtilCB(getItems(), items));

//any clear up on memory here and then
diffResult.dispatchUpdatesTo(this);

//and then, if necessary
items.clear()
items.addAll(newItems)

如果它是一个大列表,我会在主线程之外执行calculateDiff 工作。

更新listview、gridview和recyclerview的数据:

mAdapter.notifyDataSetChanged();

或者:

mAdapter.notifyItemRangeChanged(0, itemList.size());

将新数据添加到现有数据的最佳和最酷的方法是

 ArrayList<String> newItems = new ArrayList<String>();
 newItems = getList();
 int oldListItemscount = alcontainerDetails.size();
 alcontainerDetails.addAll(newItems);           
 recyclerview.getAdapter().notifyItemChanged(oldListItemscount+1, al_containerDetails);

我用不同的方式解决了同样的问题。 我没有数据。 我正在从后台线程等待它,所以从一个空列表开始。

mAdapter = new ModelAdapter(getContext(), new ArrayList<Model>());
// Then when I get data

mAdapter.update(response.getModelList());
// And update it in my adapter

public void update(ArrayList<Model> modelList){
    adapterModelList.clear();
    for (Product model: modelList) {
        adapterModelList.add(model);
    }
   mAdapter.notifyDataSetChanged();
}

就是这样。

我发现重新加载 RecyclerView 的一个非常简单的方法是调用

recyclerView.removeAllViews();

这将首先删除 RecyclerView 的所有内容,然后使用更新的值再次添加它。

这些方法对于开始使用基本的RecyclerView是有效的和好的。

private List<YourItem> items;

public void setItems(List<YourItem> newItems)
{
    clearItems();
    addItems(newItems);
}

public void addItem(YourItem item, int position)
{
    if (position > items.size()) return;

    items.add(item);
    notifyItemInserted(position);
}

public void addMoreItems(List<YourItem> newItems)
{
    int position = items.size() + 1;
    newItems.addAll(newItems);
    notifyItemChanged(position, newItems);
}

public void addItems(List<YourItem> newItems)
{
    items.addAll(newItems);
    notifyDataSetChanged();
}

public void clearItems()
{
    items.clear();
    notifyDataSetChanged();
}

public void addLoader()
{
    items.add(null);
    notifyItemInserted(items.size() - 1);
}

public void removeLoader()
{
    items.remove(items.size() - 1);
    notifyItemRemoved(items.size());
}

public void removeItem(int position)
{
    if (position >= items.size()) return;

    items.remove(position);
    notifyItemRemoved(position);
}

public void swapItems(int positionA, int positionB)
{
    if (positionA > items.size()) return;
    if (positionB > items.size()) return;

    YourItem firstItem = items.get(positionA);

    videoList.set(positionA, items.get(positionB));
    videoList.set(positionB, firstItem);

    notifyDataSetChanged();
}

您可以在 Adapter Class 内或在 Fragment 或 Activity 中实现它们,但在这种情况下,您必须实例化 Adapter 以调用通知方法。 就我而言,我通常在适配器中实现它。

我强烈建议您使用[DiffUtil.ItemCallback][1]来处理RecyclerView.Adapter的更改:

fun setData(data: List<T>) {
    val calculateDiff = DiffUtil.calculateDiff(DiffUtilCallback(items, data))
    items.clear()
    items += data
    calculateDiff.dispatchUpdatesTo(this)
}

AdapterListUpdateCallback它使用AdapterListUpdateCallback处理大部分事情:

/**
 * ListUpdateCallback that dispatches update events to the given adapter.
 *
 * @see DiffUtil.DiffResult#dispatchUpdatesTo(RecyclerView.Adapter)
 */
public final class AdapterListUpdateCallback implements ListUpdateCallback {
    @NonNull
    private final RecyclerView.Adapter mAdapter;

    /**
     * Creates an AdapterListUpdateCallback that will dispatch update events to the given adapter.
     *
     * @param adapter The Adapter to send updates to.
     */
    public AdapterListUpdateCallback(@NonNull RecyclerView.Adapter adapter) {
        mAdapter = adapter;
    }

    /** {@inheritDoc} */
    @Override
    public void onInserted(int position, int count) {
        mAdapter.notifyItemRangeInserted(position, count);
    }

    /** {@inheritDoc} */
    @Override
    public void onRemoved(int position, int count) {
        mAdapter.notifyItemRangeRemoved(position, count);
    }

    /** {@inheritDoc} */
    @Override
    public void onMoved(int fromPosition, int toPosition) {
        mAdapter.notifyItemMoved(fromPosition, toPosition);
    }

    /** {@inheritDoc} */
    @Override
    public void onChanged(int position, int count, Object payload) {
        mAdapter.notifyItemRangeChanged(position, count, payload);
    }
}

很久之后我得到了答案:

SELECTEDROW.add(dt);
notifyItemInserted(position);
SELECTEDROW.remove(position);
notifyItemRemoved(position);

我会使用 Kotlin 适配器,

class ProductAdapter(var apples: List<Apples>?= null) : RecyclerView.Adapter<ProductViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {...}

    override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {...}

    override fun getItemCount(): Int {...}

    fun setData(newApples: List<Apples>) {
        apples = newApples
    }
}

在片段/活动中

val appleAdapter = ProductAdapter()

val recyclerView = binding.appleRecycler // or findViewById or synthetics or whatever. 
recyclerView.adapter = appleAdapter
recyclerView.layoutManager = LinearLayoutManager(requireContext())

现在处理片段中的数据变化

fun updateRecyclerData(apples: List<Apples>){
    adapter.setData(apples)
    adapter.notifyDataSetChanged()
}

updateRecyclerData(applesList)

如果上述评论中没有提到对您有用,则可能意味着问题出在其他地方。

我发现解决方案的一个地方是我将列表设置到适配器的方式。 在我的活动中,列表是一个实例变量,当任何数据更改时,我会直接更改它。 由于它是一个参考变量,所以发生了一些奇怪的事情。

所以我将引用变量更改为本地变量并使用另一个变量更新数据,然后传递给前面答案中提到的addAll()函数。

这工作正常试试吧。

  ArrayList.remove(position);
  notifyItemRemoved(position);
  notifyDataSetChanged();

我建议您探索DiffUtil 它还在处理列表更新时提高了 RecyclerView 的性能。

  1. 在您的适配器中定义一个变量:

     differList = AsyncListDiffer(this, this.callback); differList.submitList(list)

在这里,列表可以是您的初始原始列表,也可以只是一个空列表,前提是您稍后将对其进行更新。

  1. 实现回调:

     private val callback : DiffUtil.ItemCallback<Item> = object: DiffUtil.ItemCallback<Item>() { override fun areItemsTheSame(oldItem: Item, newItem: Item) = oldItem.id == newItem.id override fun areContentsTheSame(oldItem: Item, newItem: Item) = oldItem == newItem }
  2. 此外,在同一个适配器中,您将有一些公共函数来设置列表。

     fun setData(list: List<Item>) { differList.submitList(list) //yeah, that's it! }
  1. 现在,在对列表(插入/更新/删除)进行任何更改后,只需从片段/活动中调用此setData(list)

    mAdapter.setData(list)

很简单,对吧?

暂无
暂无

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

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