RecyclerView multiple selections and ActionMode

608 Views Asked by At

I'm working on a app that uses RecyclerView and ActionMode. On the RecyclerView item I have an Imageview (mMultipleSelectionBackground) that is set to GONE. Basically when I use long click / click on an item it will select it and highlight it (changing the ImageView to visible) (if multiple items are selected, it will change the ImageView to Visible on each particular one). I'm doing this in the Adapter class.

@Override
    public boolean onLongClick(View view) {
        int longClickedPosition = getAdapterPosition();
        mLongClickListener.onToDoLongClick(longClickedPosition);
        ToDo toDo = mToDos.get(longClickedPosition);
        try {
            /**
             * Check to see if the item is selected
             * @mMultipleSelections - use this to block the long click if the user already did it on an item
             */
            if (!toDo.isSelected() && mMultipleSelections <= 0){
                toDo.setSelected(true);
                mMultipleSelections++;
                selectedToDos.add(longClickedPosition);
                mMultipleSelectionBackground.setVisibility(View.VISIBLE);
                view.startActionMode(mActionModeCallback);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

So far so good. The issue that I'm having is that when I'm using the onDestroyActionMode, I want the ImageView on all of the items to be reverted to GONE, but they are not. Only the first one is changed(and indeed, by this logic, it is normal).

@Override
        public void onDestroyActionMode(ActionMode actionMode) {
            Log.d(LOG_TAG, "DESTROY");
            for (int x = 0; x < selectedToDos.size(); x++) {
                mMultipleSelectionBackground.setVisibility(View.GONE);
                mToDos.get(x).setSelected(false);
            }
            mMultipleSelections = 0;
        }

My question is, how do I change the ImageView on all the items and not only the first one?

1

There are 1 best solutions below

1
On BEST ANSWER

It will be better if you add one more extra boolean field inside your ToDo class which will be false by default for checking selection of that entry like

class ToDo{
...
private boolean isSelected;

public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }
...
}

So when you click or LongClick on your item just make that boolean value true/false accordingly and based on that value inside your adapter onBindViewHolder write following code

@Override
    public void onBindViewHolder(@NonNull final UserViewHolder userViewHolder, int position) {
        final Todo todo=mTodos.get(position);            
        if(todo.isSelected()){
            viewHolder.imageView.setVisibility(View.VISIBLE);
        }else{
            viewHolder.imageView.setVisibility(View.GONE);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                todo.setSelected(!todo.isSelected());
                notifyDataSetChanged();
            }
        });
    }

So Overall this boolean field will help you to easily manage your selection entries.