Skip to content

Instantly share code, notes, and snippets.

@kundansviet
Last active August 11, 2018 13:05
Show Gist options
  • Save kundansviet/008c9024e8a99bf75b9eb19435ffc476 to your computer and use it in GitHub Desktop.
Save kundansviet/008c9024e8a99bf75b9eb19435ffc476 to your computer and use it in GitHub Desktop.
Recyclerview with checkbox
package com.cb.recyclerwithcheckbox;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import java.util.HashMap;
import java.util.List;
/**
* Created by kundan on 1/14/2017.
*/
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> {
private HashMap<Integer, Boolean> isChecked = new HashMap<>();
private Context context_1;
private LayoutInflater inflater;
private List<String> product_list;
public ListAdapter(Context context, List<String> p_list) {
this.context_1 = context;
this.product_list = p_list;
inflater = LayoutInflater.from(context);
}
@Override
public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View convertview = inflater.inflate(R.layout.single_row_with_check, parent, false);
ListHolder holder = new ListHolder(convertview);
return holder;
}
@Override
public void onBindViewHolder(ListHolder holder, int position) {
holder.cb_product.setText(product_list.get(position));
//check check box is already checked or not because recyclerview use recycling method to reuse view
if (isChecked.containsKey(position)) {
holder.cb_product.setChecked(isChecked.get(position));
} else {
holder.cb_product.setChecked(false);
}
}
@Override
public int getItemCount() {
return product_list.size();
}
public class ListHolder extends RecyclerView.ViewHolder {
CheckBox cb_product;
public ListHolder(View itemView) {
super(itemView);
cb_product = (CheckBox) itemView.findViewById(R.id.cb_product);
cb_product.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//save checked data in hash map on check change
isChecked.put(getAdapterPosition(), b);
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment