1

I wanna show all check box in all item of recycler view . when i long click on them. I will show you what i want to do in this image.

enter image description here

enter image description here

When i long click on the picture folder in other folders a empty check box will appear then we can choose them and do what we want to them. at the end how can i do this when i click on item and this happen.

this my adapter .

public class NoteAdapter extends Adapter<NoteAdapter.MyView> {
List<Note> notes;
Activity activity1;


public NoteAdapter(List<Note> noteList, Activity activity) {
    activity1 = activity;
    notes = noteList;
}

@NonNull
@Override
public NoteAdapter.MyView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_note, parent, false);
    return new MyView(itemView);
}

@Override
public void onBindViewHolder(@NonNull final NoteAdapter.MyView holder, final int position) {
    holder.note.setText(notes.get(position).note);
    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            holder.checkbox.setVisibility(View.VISIBLE);
            //the code at the line up just set visible the selected item but i want to set visible all items check boxs
            return false;
        }
    });
}

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

public class MyView extends RecyclerView.ViewHolder {
    private TextView name;
    private CheckBox checkBox;

    public MyView(@NonNull View itemView) {
        super(itemView);
        note = itemView.findViewById(R.id.note);
        chackbox = itemView.findViewById(R.id.chackBox);
    }
}

}

tohid noori
  • 221
  • 1
  • 11

1 Answers1

1

You can try this:

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.MyView> {
List< Note > notes;
Activity activity1;
ArrayList<Integer> checkedList = new ArrayList<>();
RecyclerView recyclerView;

//.....

@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    this.recyclerView = recyclerView;
}

@Override
public void onBindViewHolder(@NonNull final NoteAdapter.MyView holder, final int position) {
    holder.name.setText(notes.get(position).note);
    holder.checkBox.setVisibility(checkedList.size() > 0 ? View.VISIBLE : View.GONE);
    holder.checkBox.setChecked(checkedList.contains(position));

}

//......

public class MyView extends RecyclerView.ViewHolder {
    private TextView name;
    private CheckBox checkBox;

    public MyView(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);
        checkBox = itemView.findViewById(R.id.checkBox);

        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(checkedList.contains(getAdapterPosition())){
                    checkedList.remove(Integer.valueOf(getAdapterPosition()));
                    if (checkedList.size() == 0){
                        notifyDataSetChanged();
                    }else {
                        notifyItemChanged(getAdapterPosition());
                    }
                }else {
                    checkedList.add(getAdapterPosition());
                    if (checkedList.size() == 1){
                        recyclerView.post(new Runnable()
                        {
                            @Override
                            public void run() {
                                notifyDataSetChanged();
                            }
                        });
                    }else {
                        notifyItemChanged(getAdapterPosition());
                    }

                }
                //the code at the line up just set visible the selected item but i want to set visible all items check boxs
                return false;
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    if(!checkedList.contains(getAdapterPosition()))
                        checkedList.add(getAdapterPosition());
                }else{
                    checkedList.remove(Integer.valueOf(getAdapterPosition()));
                    if (checkedList.size() == 0){
                        recyclerView.post(new Runnable()
                        {
                            @Override
                            public void run() {
                                notifyDataSetChanged();
                            }
                        });
                    }
                }
            }
        });
    }
  }
}

I added long click method in the MyView and I made some changes. It works more stable. There is a position error in the onBind method.

onAttachedToRecyclerView : some times notifyDataSetChanged() method gives error. I added to be able to use recyclerView

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35