MultiChoiceRecyclerView
MultiChoiceRecyclerView copied to clipboard
Option to deny some positions from being selected
I'd like to prevent some entries in my list from being selected. It would be nice to have an interface on the adapter which positions are "disabled" (or provide an array of positions or something like this).
The feature is already there, just override protected boolean isSelectableInMultiChoiceMode(int position) and dictate the rules.
https://github.com/dvdciri/MultiChoiceRecyclerView/blob/master/library/multichoicerecyclerview/src/main/java/com/davidecirillo/multichoicerecyclerview/MultiChoiceAdapter.java#L57
Ah great - didn't see that.
But now I have the problem, that the single-click ist passed to disabled entries while others are selected. How can I achieve, that disabled entries to nothing when clicked on them while a multi-selection is ongoing?
Sry, can't think today ;) defaultItemViewClickListener() does the trick of course.
Well no, it does not... When returning null in defaultItemViewClickListener() for positions I'd like to be not-selectable, the normal click (when no selection is going on) is disabled too.
So the problem is still here: How can I disable some positions from being multi-selected while have the normal single-click active (but only when no multi-selection is ongoing).
Plus, the long-press is still active for entries marked with false in isSelectableInMultiChoiceMode(). Seems like a bug!?
I'm gonna reopen this and take a look at this once got time, thanks
BTW, I managed to use a workaround for this.
My adapters extend from a man-in-the-middle adapter MultiChoiceAdapterDelegate which has a flag multiSelectEnabled along with a setter for it..
public class MultiChoiceAdapterDelegate<MODEL> extends MultiChoiceAdapter<RecyclerView.ViewHolder> {
private boolean multiSelectEnabled = true;
The flag is checked in onBindViewHolder() and the original method of this library (super.onBindViewHolder()) is only called, when the flag is set to true. If Not, the defaultItemViewClickListener() is applied directly (instead of letting the library decide what to do).
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
/*
* When MultiSelect is disabled, do not pass-trough
* so the longpress-listener won't be set.
*
* But we then need to set the normal click-listener ourselves.
*/
if (multiSelectEnabled) {
super.onBindViewHolder(holder, position);
} else {
holder.itemView.setOnClickListener(defaultItemViewClickListener(holder, position));
}
adapter.onBindViewHolder(holder, position);
}
But this is just a workaround and this issue asks for a way to have this in the library. Maybe we can just include this flag and method in MultiChoiceAdapter directly (no time to test it myself but looks like it should work flawlessly).