I\'m trying to refresh specific item in RecyclerView
.
Story: Whenever user clicks on item, it shows AlertDialog
. User can
you just have to add following code in alert dialog box on save click
recyclerData.add(position, updateText.getText().toString());
recyclerAdapter.notifyItemChanged(position);
notifyItemChanged(updateIndex)
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.
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.
A way that has worked for me personally, is using the recyclerview's adapter methods to deal with changes in it's items.
It would go in a way similar to this, create a method in your custom recycler's view somewhat like this:
public void modifyItem(final int position, final Model model) {
mainModel.set(position, model);
notifyItemChanged(position);
}
The problem is RecyclerView.Adatper
does not provide any methods that return the index of element
public abstract static class Adapter<VH extends ViewHolder> {
/**
* returns index of the given element in adapter, return -1 if not exist
*/
public int indexOf(Object elem);
}
My workaround is to create a map instance for (element, position)s
public class FriendAdapter extends RecyclerView.Adapter<MyViewHolder> {
private Map<Friend, Integer> posMap ;
private List<Friend> friends;
public FriendAdapter(List<Friend> friends ) {
this.friends = new ArrayList<>(friends);
this.posMap = new HashMap<>();
for(int i = 0; i < this.friends.size(); i++) {
posMap.put(this.friends.get(i), i);
}
}
public int indexOf(Friend friend) {
Integer position = this.posMap.get(elem);
return position == null ? -1 : position;
}
// skip other methods in class Adapter
}
Friend
) should implements hashCode()
and equals()
because it is key in hashmap.when an element changed,
void someMethod() {
Friend friend = ...;
friend.setPhoneNumber('xxxxx');
int position = friendAdapter.indexOf(friend);
friendAdapter.notifyItemChanged(position);
}
It is good to define an helper method
public class FriendAdapter extends extends RecyclerView.Adapter<MyViewHolder> {
public void friendUpdated(Friend friend) {
int position = this.indexOf(friend);
this.notifyItemChanged(position);
}
}
Map instance(Map<Friend, Integer> posMap
) is not necessarily required.
If map is not used, looping throughout list can find the position of an element.
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!