如何更新/刷新 RecyclerView 中的特定项目 [英] How to update/refresh specific item in RecyclerView

查看:30
本文介绍了如何更新/刷新 RecyclerView 中的特定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试刷新 RecyclerView 中的特定项目.

I'm trying to refresh specific item in RecyclerView.

故事:每当用户点击项目时,它会显示AlertDialog.用户可以通过单击确定按钮键入一些文本.我想在此项目中显示此文本并显示不可见的 ImageView - 在 XML 和适配器中声明 ViewHolder -

Story: Whenever user clicks on item, it shows AlertDialog. User can type some text by clicking ok button. I want to show this text in this item and show invisible ImageView - declared in XML and adapter ViewHolder -

我在 AlertDialog Positive Button 中使用了这个函数来更新项目:

I used this function in AlertDialog Positive Button to update the item:

private void updateListItem(int position) {
  View view = layoutManager.findViewByPosition(position);
  ImageView medicineSelected = (ImageView) view.findViewById(R.id.medicine_selected);
  medicineSelected.setVisibility(View.VISIBLE);
  TextView orderQuantity = (TextView) view.findViewById(R.id.order_quantity);
  orderQuantity.setVisibility(View.VISIBLE);
  orderQuantity.setText(quantity + " packet added!");

  medicinesArrayAdapter.notifyItemChanged(position);
}

但是这段代码不仅改变了传递位置的itemView,还改变了其他一些itemView!

But this code not only changes the itemView at passed position, but also changes some of other itemView(s) as well!

我应该如何通过单击来正确更改特定的 itemView?

How should I change specific itemView correctly by clicking on it?

推荐答案

您可以使用 notifyItemChanged(int position) 方法来自 RecyclerView.Adapter 类.来自文档:

You can use the notifyItemChanged(int position) method from the RecyclerView.Adapter class. From the documentation:

通知任何已注册的观察者位置上的项目已更改.相当于调用notifyItemChanged(position, null);.

Notify any registered observers that the item at position has changed. Equivalent to calling notifyItemChanged(position, null);.

这是一个项目更改事件,而不是结构更改事件.它表示位置数据的任何反映都已过时并且应该更新.位置的项目保持相同的身份.

This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity.

既然你已经有了这个职位,它应该适合你.

As you already have the position, it should work for you.

这篇关于如何更新/刷新 RecyclerView 中的特定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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