如何在多片段活动中处理 onContextItemSelected? [英] How to handle onContextItemSelected in a multi fragment activity?

查看:19
本文介绍了如何在多片段活动中处理 onContextItemSelected?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试调整我的应用程序以使用Android v4 兼容性库",以便为 Android 1.6 用户提供使用片段的好处.

I'm currently trying to adapt my application to use the "Compatibility Libraries for Android v4" to provide the benefits of the usage of fragments even to Android 1.6 users.

上下文菜单的实现似乎很棘手:

The implementation of a context menu seems to be tricky:

  • 应用的主要活动正在扩展 FragmentActivity类.
  • 片段都是基于一个扩展 Fragment 类的类.
  • 片段类正在调用registerForContextMenu() 在其 onCreateView() 方法中并覆盖这些方法onCreateContextMenu()onContextItemSelected().
  • The main activity of the application is extending the FragmentActivity class.
  • The fragments are all based on one class which extends the Fragment class.
  • The fragment class is calling registerForContextMenu() in its onCreateView() method and overrides the methods onCreateContextMenu() and onContextItemSelected().

对于 onCreateContextMenu() 这很有效.上下文菜单从资源文件扩展而来,并根据所选项目(基于 listView...即使片段不是 ListFragment)略有修改.

For onCreateContextMenu() this works pretty well. The context menu is inflated from a resource file and slightly modified based on the selected item (which is based on a listView... even if the fragment is not an ListFragment).

选择上下文菜单条目时会出现问题.onContextItemSelected() 为所有当前存在的片段调用,从第一个添加的片段开始.

The problem occurs when a context menu entry is selected. onContextItemSelected() is called for all currently existing fragments starting with the first added one.

就我而言,片段用于显示文件夹结构的内容.当打开子文件夹片段的上下文菜单并选择菜单项时,首先在上层调用 onContextItemSelected()(取决于此时允许/可见的片段数量).

In my case the fragments are used to show the content of a folder structure. When the context menu of a subfolder fragment is opened and a menu item is selected, onContextItemSelected() is first called on the upper levels (depending on how many fragments are allowed/visible in this moment).

现在,我在活动级别使用一个字段的变通方法,该字段包含调用其 onCreateContextMenu() 的最后一个片段的标签.这样,当存储的标签与 getTag() 不同时,我可以在 onContextItemSelected() 的开头调用return super.onContextItemSelected(item)".但这种方法在我看来有点脏.

Right now, I use a workaround by a field on activity level which holds the tag of last fragment calling its onCreateContextMenu(). This way I can call "return super.onContextItemSelected(item)" in the begin of onContextItemSelected() when the stored tag is not the same as getTag(). But this approach looks a bit dirty to me.

为什么对所有片段都调用 onContextItemSelected()?而不仅仅是调用 onCreateContextMenu() 的那个?

Why is onContextItemSelected() called on all fragments? and not just one the one that was calling onCreateContextMenu()?

处理这个问题最优雅的方法是什么?

What is the most elegant way to handle this?

推荐答案

我找到了替代方案.它对我上面的问题没有任何改变,但它使它变得毫无意义.

I found an alternative. It does not change anything on my problem above, but it makes it pointless.

我已经从我的应用程序中完全删除了上下文菜单.相反,我捕获列表项上的长按,并在此时更改操作栏的可见按钮.从用户的角度来看,这更像是一个上下文菜单.

I have remove the context menu completely from my application. Instead I capture the longclick on a list item and change the visible buttons of the action bar in this moment. From the user point of view this is much more tablet like as a context menu.

在向后兼容的应用程序中,操作栏不存在.所以我决定为 Honeycomb 之前的设备构建我自己的(顶部的工具栏).

In backward compatible applications the actionbar does not exist. So I've decided to build my own (kind of toolbar on top) for the devices pre Honeycomb.

如果您想继续使用上下文菜单,我没有找到比上面提到的解决方法更好的解决方案.

If you would like to stay with the context menu, I did not find a better solution as the workaround I've mentioned above.

这篇关于如何在多片段活动中处理 onContextItemSelected?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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