可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to refresh specific item in RecyclerView.
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 -
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); }
But this code not only changes the itemView at passed position, but also changes some of other itemView(s) as well!
How should I change specific itemView correctly by clicking on it?
回答1:
You can use the notifyItemChanged(int position) method from the RecyclerView.Adapter class. From the documentation:
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.
回答2:
I think I have an Idea on how to deal with this. Updating is the same as deleting and replacing at the exact position. So I first remove the item from that position using the code below:
public void removeItem(int position){ mData.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position, mData.size()); }
and then I would add the item at that particular position as shown below:
public void addItem(int position, Landscape landscape){ mData.add(position, landscape); notifyItemInserted(position); notifyItemRangeChanged(position, mData.size()); }
I'm trying to implement this now. I would give you a feedback when I'm through!
回答3:
Update single item
- Update the data item
- Notify the adapter of the change with
notifyItemChanged(updateIndex)
Example
Change the "Sheep" item so that it says "I like sheep."

String newValue = "I like sheep."; int updateIndex = 3; data.set(updateIndex, newValue); adapter.notifyItemChanged(updateIndex);
My full answer with more examples is here.
回答4:
I got to solve this issue by catching the position of the item that needed to be modified and then in the adapter call
public void refreshBlockOverlay(int position) { notifyItemChanged(position); }
, this will call onBindViewHolder(ViewHolder holder, int position) for this specific item at this specific position.
回答5:
That's also my last problem. Here my solution I use data Model and adapter for my RecyclerView
**Firstly, register your new data to your model** DataModel detail = new DataModel(id, name, sat, image); **after that, use set to replace old value with the new one** mData.set(index, detail); **finally, refresh your adapter** if(adapter!=null) adapter.notifyItemChanged(index);
回答6:
In your adapter class, in onBindViewHolder method, set ViewHolder to setIsRecyclable(false) as in below code.
@Override public void onBindViewHolder(RecyclerViewAdapter.ViewHolder p1, int p2) { // TODO: Implement this method p1.setIsRecyclable(false); // Then your other codes }
回答7:
In your RecyclerView adapter, you should have an ArrayList and also one method addItemsToList(items) to add list items to the ArrayList. Then you can add list items by call adapter.addItemsToList(items) dynamically. After all your list items added to the ArrayList then you can call adapter.notifyDataSetChanged() to display your list.
You can use the notifyDataSetChanged in the adapter for the RecyclerView