Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifyItemChanged(int position) updates multiple items in RecyclerView

I Am using RecyclerView for displaying a list of items, and I need to update state for single item by position. I am updating my item in list then call notifyItemChanged(int position) like so:

myList.set(position, newModifiedItem);
notifyItemChanged(position);

The item is updated successfully but also are randomly updated some other items, and each time I scroll up and down through my item list, this updates (different icon state) is made on other items too.

I am doing changes in onBindViewHolder where I check list item by position and decide to set different status.

Full Adapter code:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<MyObj> myList;

    public MyAdapter(List<MyObj> list) {
        this.myList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_item, parent, false);
        return new ViewHolder(view);
    }

    public void onBindViewHolder(final ViewHolder holder, final int position) {
        MyObj myObj = myList.get(position);
        boolean isSpecial = myObj.getMySpecialStatus();

        holder.myItemTitle.setText(myObj.getTitle());

        //decide if item has different icon state
        if (isSpecial) {
            holder.myItemIcon.setImageResource(R.drawable.ic_special);    
        } 
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView myItemTitle;
        public ImageView myItemIcon;
        public LinearLayout itemClickable;
        //some other views

        public ViewHolder(View itemView) {
            super(itemView);
            myItemTitle = (TextView) itemView.findViewById(R.id.item_title);
            myItemIcon = (ImageView) itemView.findViewById(R.id.item_img);

            itemClickable = (LinearLayout) itemView.findViewById(R.id.item_clickable_zone);
            itemClickable.setOnClickListener(this);

            //some other views
        }

        @Override
        public void onClick(View v) {
            final int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {

                switch (v.getId()) {
                    case R.id.item_clickable_zone: {

                        //some logic to change status - here just an idea
                        MyObj newModifiedItem = myList.get(position);
                        newModifiedItem.setMySpecialStatus(true);

                        myList.set(position, newModifiedItem);
                        notifyItemChanged(position);
                        break;
                    }

                    //some other views on click
                }
            }
        }
    }
}
like image 302
Vasile Doe Avatar asked Sep 06 '16 07:09

Vasile Doe


1 Answers

The problem is in your onBindViewHolder() where you didn't use else case for if statement. It shoult be like:

public void onBindViewHolder(final ViewHolder holder, final int position) {
        MyObj myObj = myList.get(position);
        boolean isSpecial = myObj.getMySpecialStatus();

        holder.myItemTitle.setText(myObj.getTitle());

        //decide if item has different icon state
        if (isSpecial) {
            holder.myItemIcon.setImageResource(R.drawable.ic_special);    

         } else {
            //here is the trick - set normal state back
            holder.myItemIcon.setImageResource(R.drawable.ic_standard);
         }
    }
like image 87
Choletski Avatar answered Nov 25 '22 18:11

Choletski