unregisterForContextMenu可以完全注销视图吗? [英] Can unregisterForContextMenu completely unregister a View?

查看:98
本文介绍了unregisterForContextMenu可以完全注销视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,可以有一个上下文菜单,也可以没有。我分别使用 registerForContextMenu unregisterForContextMenu ,注册本身或与上下文菜单相关的任何显示都没有问题/ working。

I have a ListView, which can either have a context menu, or not. I am using registerForContextMenu and unregisterForContextMenu respectively and have no problems with the registration itself or anything related to the context menu showing up/working.

为上下文菜单注册视图后,当用户单击它时,其背景逐渐从橙色变为黄色,然后变为白色。如果从未为上下文菜单注册视图(它保持相同的颜色),则不会发生这种情况。但是,当我使用 unregisterForContextMenu 取消注册相关视图时,此效果仍然有效,并且可能使用户感到困惑-由于该效果,他/她将期望使用上下文菜单。这表明 unregisterForContextMenu 不能完全逆转 registerForContextMenu 的效果。

When a view is registered for a context menu, when the user clicks it, its background changes gradually from orange through yellow and then to white. This does not happen when a view has never been registered for a context menu (it stays the same color). However, when I use unregisterForContextMenu to unregister the view in question, this effect is still active and may confuse the user - he/she will expect a context menu because of the effect. This shows that unregisterForContextMenu does not completely reverse the effect of registerForContextMenu.

我的问题是:是否可以完全取消注册视图以获取上下文菜单?

My question is: Is there a way to completely unregister a View from getting a context menu?

如果有人对(un)registerForContextMenu 在内部如何工作有更清晰的了解,请分享您的想法。

If someone has a clearer idea of how (un)registerForContextMenu works internally, please share your thoughts.

编辑:我刚刚看到这不适用于HTC的感应界面,因为那里没有实现增白效果。因此,该问题仅适用于默认的Android界面。

I just saw that this does not apply to HTC's sense interface, as there the 'whitening' effect is not implemented. So the question applies only to the default Android interface.

推荐答案

事实证明非常简单。 registerForContextMenu(View v)只是调用 v.setOnCreateContextMenuListener(this),将调用Activity称为 OnCreateContextMenuListener 。问题在于,在View类中, setOnCreateContextMenuListener 正在检查视图是否可长时间单击,如果不是,则将其设置为。但是,方法 unregisterForContextMenu(视图v)仅调用 v.setOnCreateContextMenuListener(空)。但是 setOnCreateContextMenuListener 不会检查传递的 OnCreateContextMenuListener 是否为空,因此始终使视图存在问题

Turned out to be very simple. registerForContextMenu (View v) is simply calling v.setOnCreateContextMenuListener (this), referring to the calling Activity as the OnCreateContextMenuListener. The problem is that in the View class, the setOnCreateContextMenuListener is checking if the view is long-clickable, and if it isn't, it sets it to be. However, the method unregisterForContextMenu (View v) only calls v.setOnCreateContextMenuListener (null). But the setOnCreateContextMenuListener doesn't check whether the passed OnCreateContextMenuListener is null or not, thereby always leaving the view in question long-clickable.

解决方案是仅手动调用 v.setLongClickable(假)

The solution was to just call v.setLongClickable (false) manually.

这篇关于unregisterForContextMenu可以完全注销视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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