我正在尝试刷新 RecyclerView
中的特定项目。
故事:当用户点击项目时,它会显示AlertDialog
。用户可以通过单击确定按钮键入一些文本。我想在此项目中显示此文本并显示不可见的 ImageView
- 在 XML 和适配器中声明 ViewHolder
-
我在AlertDialog
中使用了这个函数来更新项目的正按钮:
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!
我应该如何通过点击正确更改特定的itemView?
最佳答案
您可以使用 notifyItemChanged(int position)
来自 RecyclerView.Adapter 类的方法。来自文档:
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.
既然你已经有了这个职位,它应该适合你。
关于android - 如何更新/刷新 RecyclerView 中的特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457406/