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

查看:149
本文介绍了如何在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肯定按钮中使用了此功能来更新项目:

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?

推荐答案

您可以使用

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天全站免登陆