How to perform multiselection in recyclerview?

Nitesh Mishra picture Nitesh Mishra · Jul 29, 2016 · Viewed 9.6k times · Source

I am making an app with personalisation feature where user can select his preference and based on that app will show data. on click of item it should be selected (making item border highlighted) and on re-click it should be unselected also there should be multiselection in recyclerview.here is the screen shot of my app's personalisation screen

enter image description here

Answer

Alok Omkar picture Alok Omkar · Jul 29, 2016

Adding to sonnv1368's answer :

Try this :

Create a boolean in your model class object to keep track of selection of imageview : say boolean isSelected;

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Object item = objectList.get(position);
    holder.imageView.setSelected(item.getSelected());

}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ImageView imageView;
    public ViewHolder(View view) {
      //initialization code here
      imageView.setOnClickListener(this);
    }

    @Override
    public void onClick( View view ) {
      Object object = objectList.get(getAdapterPosition());
      object.isSelected() ? object.setSelected(false) : object.setSelected(true);
      notifyItemChanged(getAdapterPosition());
    }
}