问题
I have a customized GridView populated by a customized BaseAdapter. The selection mode of the GridView is MultiChoiceModal. I want to control which items can be activated when long clicked while still ensuring they respond to (short) click events. BaseAdapter has a method called isEnabled
, the perfect solution would be if it had a method isActivatable
that behaved analogously. The next best solution would be to intercept long clicks and pass them along to the default handler only when acceptable.
Here are some things that don't work:
Overriding
isEnabled
in the adapter. This is overkill. In that case, the items cease responding to click events.Calling
setLongClickable
for the parent view of each item in the adapter'sgetView
method. This is fraught with problems even if it worked. Needless to say it doesn't. Likely a byproduct of the selection mode.Setting a custom
onLongClickListener
for the parent view of each item in the adapter'sgetView
method. Android Studio suggests against this when using any AdapterView. It suggests overridingonItemLongClick
instead.Overriding
onItemLongClick
in the GridView. Evidently, that is also handled for you when in this selection mode.Setting a custom
onItemLongClickListener
in the GridView.
While the hive works on this, I am going to try aborting the action mode's creation/blocking activation of prohibited items in the onItemCheckedStateChanged
method of my AbsListView.MultiChoiceModeListener. Clearly I'm running low on ideas.
回答1:
I have found a simple solution:
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
or
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
where view
is the item where you want to prevent a choice mode activation after a long click.
来源:https://stackoverflow.com/questions/27873449/how-can-i-prevent-activation-for-some-listview-items-when-the-selection-mode-is