错误的片段ViewPager接收onContextItemSelected电话片段、错误、电话、ViewPager

2023-09-12 09:24:58 作者:無人問津㈠ㄍㄑ

我有一个应用程序,显示了一些片段(同类型)的 ViewPager 和我有上下文菜单项一些麻烦。 (我使用的支持库)。

I have an app that shows a few fragments (of the same type) in a ViewPager and I'm having some trouble with context menu items. (I'm using the support library).

在上下文菜单中选择右键菜单中的片段之一,错误的片段接收 onContextItemSelected 事件中调用。

When a context menu item is selected in the context menu in one of the fragments, the wrong fragment is receiving the onContextItemSelected event call.

例如,如果我在我的寻呼机上的片段#3,该片段在位置#2接收它代替。如果我刷回片段#2,碎片#3接收调用。

For example, if I'm on fragment #3 in the pager, the fragment at position #2 receives it instead. If I swipe back to fragment #2, fragment #3 receives the call instead.

我有一个样品这里。

(我目前正在围绕这在我自己的应用程序通过在每个片段 mHandleContext 变量和启用/禁用它时,页面被更改。这样的 onContextItemSelected 通话将走出去所有碎片,直到合适的人叫。)

(I'm currently working around this in my own app by having a mHandleContext variable in each fragment and enabling/disabling it when the page is changed. That way the onContextItemSelected call will go out to all the fragments until the right one is called.)

我是不是做错了什么或者这是与支持库中的错误?作为一个方面说明,这并没有发生,当我使用ActionBarSherlock 3.5.1,它有它自己的支持库叉。

Am I doing something wrong or is this a bug with the support library? As a side note, this didn't happen when I was using ActionBarSherlock 3.5.1, which had its own fork of the support library.

推荐答案

因此​​,这是某种愚蠢的设计决策由谷歌或刚刚走了完全不值得考虑的事情。要解决这个最简单的方法是包装与if语句像这样 onContextItemSelected 电话:

So this is some sort of idiotic design decision by Google or something that has just gone totally unconsidered. The simplest way to work around this is to wrap the onContextItemSelected call with an if statement like this:

if (getUserVisibleHint()) {
    // Handle menu events and return true
} else
    return false; // Pass the event to the next fragment

在ActionBarSherlock 3.5兼容性库有这样的黑客攻击。

The compatibility library in ActionBarSherlock 3.5 had a hack like this.