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

查看:83
本文介绍了如何处理多片段活动中的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(),效果很好.将从资源文件中弹出上下文菜单,并根据所选项目(即使该片段不是ListFragment,也基于listView ...)对其进行稍加修改.

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.

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

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天全站免登陆