How to update/refresh specific item in RecyclerView

前端 未结 13 1292
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 18:31

I\'m trying to refresh specific item in RecyclerView.

Story: Whenever user clicks on item, it shows AlertDialog. User can

相关标签:
13条回答
  • 2020-11-29 18:45

    you just have to add following code in alert dialog box on save click

              recyclerData.add(position, updateText.getText().toString());
              recyclerAdapter.notifyItemChanged(position);
    
    0 讨论(0)
  • 2020-11-29 18:46

    Update single item

    1. Update the data item
    2. Notify the adapter of the change with notifyItemChanged(updateIndex)

    Example

    Change the "Sheep" item so that it says "I like sheep."

    Update single item

    String newValue = "I like sheep.";
    int updateIndex = 3;
    data.set(updateIndex, newValue);
    adapter.notifyItemChanged(updateIndex);
    

    My full answer with more examples is here.

    0 讨论(0)
  • 2020-11-29 18:49

    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.

    0 讨论(0)
  • 2020-11-29 18:52

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 18:53

    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
    }
    
    • the element type(here class 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.

    0 讨论(0)
  • 2020-11-29 18:57

    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!

    0 讨论(0)
提交回复
热议问题