How Can I Prevent Activation For Some ListView Items When The Selection Mode Is MultiChoiceModal?

試著忘記壹切 提交于 2019-12-12 17:12:16

问题


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:

  1. Overriding isEnabled in the adapter. This is overkill. In that case, the items cease responding to click events.

  2. Calling setLongClickable for the parent view of each item in the adapter's getView method. This is fraught with problems even if it worked. Needless to say it doesn't. Likely a byproduct of the selection mode.

  3. Setting a custom onLongClickListener for the parent view of each item in the adapter's getView method. Android Studio suggests against this when using any AdapterView. It suggests overriding onItemLongClick instead.

  4. Overriding onItemLongClick in the GridView. Evidently, that is also handled for you when in this selection mode.

  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!