private boolean[] selectedItems;
public MyAdapter(List
@Override public void onBindViewHolder(MyViewHolder holder, int position) { MyModel myModel = dataSet.get(position); holder.title.setText(myModel.getTitle()); holder.description.setText(myModel.getDescription()); holder.checkBox.setChecked(selectedItems[position]); // set checkbox state based on selectedItems }
public interface OnItemClickListener { void onItemClick(int position); }
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView title; public TextView description; public CheckBox checkBox;
public MyViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
description = itemView.findViewById(R.id.description);
checkBox = itemView.findViewById(R.id.checkbox);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
onItemClick.onItemClick(position);
}
}
public class MyActivity extends AppCompatActivity {
private List data;
private RecyclerView recyclerView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data = generateData();
recyclerView = findViewById(R.id.recycler_view);
adapter = new MyAdapter(data);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
selectedItems[position] = !selectedItems[position]; // invert selected state
adapter.notifyItemChanged(position);
}
});
}
private List generateData() {
// generate data
}
public void deleteSelectedItems(View view) {
// loop through selectedItems and delete corresponding items from data list
for