I added a selector background for my RecyclerView items, but I need to allow only single item selection at a time. i.e if one item is selected all the rest of the items must not be selected. How can I achieve this?
Here is my adapter class
public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
private List<VisaType> visaTypeList;
int selected_position;
public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) {
this.context = context;
this.visaTypeList = visaTypeList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false);
return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final VisaType a = visaTypeList.get(position);
final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder;
viewHolder.visa_eng.setText(a.getName_en());
viewHolder.visa_jp.setText(a.getName_jp());
final long id = a.getId();
if (id == AppSingleTon.visaType) {
viewHolder.visa_layout.setSelected(true);
} else {
viewHolder.visa_layout.setSelected(false);
}
viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolder.visa_layout.isSelected()) {
viewHolder.visa_layout.setSelected(false);
} else {
viewHolder.visa_layout.setSelected(true);
AppSingleTon.visaType = id;
selected_position = position;
}
}
});
}
}
For single selection, we just need to hold the position that clicked in the recyclerview. When user clicks the another position, forget previous one and make new position as hold position.
This is my sample solution:
public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
private List<VisaType> visaTypeList;
int selected_position;
public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) {
this.context = context;
this.visaTypeList = visaTypeList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false);
return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final VisaType a = visaTypeList.get(position);
final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder;
viewHolder.visa_eng.setText(a.getName_en());
viewHolder.visa_jp.setText(a.getName_jp());
final long id = a.getId();
if (id == AppSingleTon.visaType) {
viewHolder.visa_layout.setSelected(true);
} else {
viewHolder.visa_layout.setSelected(false);
}
if (position == selected_position) {
viewHolder.visa_layout.setSelected(true);
} else {
viewHolder.visa_layout.setSelected(false);
}
viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selected_position = getAdapterPosition();
notifyDataSetChanged();
}
});
}
}
Hope it helps you... Thanks
You can also consider usage of RecyclerView extended by SelectionTracker. Then some piece of code must be added to your current implementation to implement all needed methods. Finally in a place where you build SelectionTracker you can put code like below and inject SelectionTracker into your Adapter class.
Exapmle code of constructing SelectionTracker:
SelectionTracker selectionTracker = new SelectionTracker.Builder<>(
"example-unique-id",
mRecyclerView,
new UsersListAdapter.UserItemKeyProvider(list),
new UsersListAdapter.UserItemLookup(mRecyclerView),
StorageStrategy.createStringStorage()
).withSelectionPredicate(SelectionPredicates.<String>createSelectSingleAnything()).build();
Method:
withSelectionPredicate(SelectionPredicates.<String>createSelectSingleAnything())
is the crucial here because it defines "selection approach". String here is one of possible keys. For more information follow:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With