Each item of a RecyclerView
contains a Checkbox
, and when selecting a checkbox you would like to disable all other checkboxes.
I created setOnCheckedChangeListener
inside onBindViewHolder
and I believe I should disable the other checkboxes inside it after a checkbox is selected, making a for
and ignoring the selected one. But I do not know how to get every item in the RecyclerView and disable the checkbox.
Am I on the right track or should I implement it differently? Is there any simple way to do this?
Summarizing my scenario, I have a list and want the user to select only one item, disabling others after a selected one.
UPDATED:
ViewHolder:
public class LocalRouteSelectionViewHolder extends RecyclerView.ViewHolder {
public final TextView tvId;
public final TextView tvDate;
public final TextView tvTarget;
public final TextView tvAmount;
public final CheckBox cbRouteSelect;
public LocalRouteSelectionViewHolder(View view) {
super(view);
tvId = (TextView) view.findViewById(R.id.textview_route_selection_id);
tvDate = (TextView) view.findViewById(R.id.textview_route_selection_date);
tvTarget = (TextView) view.findViewById(R.id.textview_route_selection_target);
tvAmount = (TextView) view.findViewById(R.id.textview_route_selection_amount);
cbRouteSelect = (CheckBox) view.findViewById(R.id.checkbox_route_select);
}
}
Adapter:
public class LocalRouteListSelectionAdapter extends RecyclerView.Adapter {
private List<Route> routes;
private Context context;
Route route;
public LocalRouteListSelectionAdapter(List<Route> routes, Context context) {
this.routes = routes;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context)
.inflate(R.layout.item_localroute_selection, parent, false);
LocalRouteSelectionViewHolder holder = new LocalRouteSelectionViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
LocalRouteSelectionViewHolder holder = (LocalRouteSelectionViewHolder) viewHolder;
route = routes.get(position) ;
String year = route.getDate().substring(0,4);
String month = route.getDate().substring(5, 7);
String day = route.getDate().substring(8, 10);
String date = day + "/" + month + "/" + year;
holder.tvId.setText(String.format("%03d", route.getId()));
holder.tvDate.setText(date);
holder.tvTarget.setText(route.getTarget());
holder.tvAmount.setText(String.valueOf(route.getAmount()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Route route = routes.get(position);
Context context = view.getContext();
Intent intent = new Intent(context, RouteDetailsActivity.class);
intent.putExtra("ROUTE", route);
context.startActivity(intent);
}
});
holder.cbRouteSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// implementar aqui
}
});
}
@Override
public int getItemCount() {
return routes.size();
}
}