Checkboxes in RecyclerView in Android

Jyotishgher Astrology
By -
0

 A simple example of using checkboxes in recycler view

A recyclerview recycles components, so the view is recreated again once you scroll up and down. To solve this problem, we will first create an empty list of deliveryNoteModels that we will use to store selected orders.

Checkboxes in RecyclerView in Android

Creating a list of checkboxes isn’t a difficult mission. Adding a “select all” is arguably not too complicated as well.
But… what if we wanted to add subcategories? And… implement this in….. a recyclerView? Now this becomes a bit more complicated and confusing, and what is easier than finding a code that does exactly that with a simple explanation? So here we go…

TestActivity.java

Lets get started with layouts first TestActivity

To create each checkbox option, create a CheckBox in your layout.
 Because a set of checkbox options lets the user select multiple items,
each checkbox is managed separately, and you must register a click listener for each one.
public
class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(this));
TestAdapter adapter = new TestAdapter();
adapter.setCallback(new TestAdapter.Callback() {
@Override
public void onCheckedChanged(String item, boolean isChecked) {
// Do what you want :)
}
});
adapter.addItem("Foo");
adapter.addItem("Bar");
adapter.notifyDataSetChanged();
}
} ======================================================================================= Lets get started with layouts first TestAdapter 
TestAdapter.java
// This is your reyclerview adapter
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
// Just storing strings for example
private List<String> items = new ArrayList<>();
// A callback that gets invoked when an item is checked (or unchecked)
private Callback callback;
// Call this to add strings to the adapter
public void addItem(String item) {
items.add(item);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_with_checkbox, parent, false));
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(items.get(position));
}
// Sets the callback
public void setCallback(Callback callback) {
this.callback = callback;
}
// Callback interface, used to notify when an item's checked status changed
public interface Callback {
void onCheckedChanged(String item, boolean isChecked);
}
// Our view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
private CheckBox checkBox;
public ViewHolder(View itemView) {
super(itemView);
// Find the checkbox view in the layout
checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
}
void bind(String s) {
// Set the text
checkBox.setText(s);
// Listen to changes (i.e. when the user checks or unchecks the box)
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Invoke the callback
if(callback != null) callback.onCheckedChanged(s, isChecked);
}
});
}
}
}

To create each checkbox option, create a CheckBox in your layout. Because a set of checkbox options lets the user select multiple items, each checkbox is managed separately, and you must register a click listener for each one.


CheckBox belongs to android.widget.CheckBox class. Android CheckBox class is the subclass of CompoundButton class. It is generally used in a place where user can select one or more than choices from a given list of choices. For example, selecting hobbies.

Post a Comment

0Comments

Post a Comment (0)