选择模式为MultiChoiceModal时,如何防止某些ListView项目激活? [英] How Can I Prevent Activation For Some ListView Items When The Selection Mode Is MultiChoiceModal?

本文介绍了选择模式为MultiChoiceModal时,如何防止某些ListView项目激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由自定义BaseAdapter填充的自定义GridView. GridView的选择模式是MultiChoiceModal.我想控制长按时可以激活哪些项目,同时仍然确保它们响应(短)点击事件. BaseAdapter有一个名为isEnabled的方法,完美的解决方案是,如果它有一个行为类似的方法isActivatable.下一个最佳解决方案是拦截长按,只有在可接受的情况下,才将它们传递给默认处理程序.

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.

有些东西不起作用:

  1. 在适配器中覆盖isEnabled.这太过分了.在这种情况下,项目将停止对点击事件做出响应.

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

为适配器的getView方法中每个项目的父视图调用setLongClickable.即使有效,这也充满了问题.不用说不是.可能是选择模式的副产品.

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.

为适配器的getView方法中每个项目的父视图设置自定义onLongClickListener.当使用任何AdapterView时,Android Studio建议不要这样做.建议改用onItemLongClick.

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.

在GridView中覆盖onItemLongClick.显然,在此选择模式下,这也将为您处理.

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

在GridView中设置自定义onItemLongClickListener.

Setting a custom onItemLongClickListener in the GridView.

在此配置单元上进行工作时,我将尝试在AbsListView.MultiChoiceModeListener的onItemCheckedStateChanged方法中中止操作模式的创建/阻止禁用项目的激活.显然,我的想法不多.

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.

推荐答案

我找到了一个简单的解决方案:

I have found a simple solution:

view.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        return true;
    }
});

view.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
});

其中view是您要阻止长按后激活选择模式的项目.

where view is the item where you want to prevent a choice mode activation after a long click.

这篇关于选择模式为MultiChoiceModal时,如何防止某些ListView项目激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆