android - ViewPager 中的错误 fragment 接收 onContextItemSelected 调用

标签 android android-support-library

我有一个应用程序在 ViewPager 中显示了一些 fragment (相同类型),但我在使用上下文菜单项时遇到了一些问题。 (我正在使用支持库)。

当在其中一个 fragment 的上下文菜单中选择上下文菜单项时,错误的 fragment 正在接收 onContextItemSelected 事件调用。

例如,如果我在寻呼机中的 fragment #3 上,则位置 #2 的 fragment 会接收它。如果我滑回 fragment #2, fragment #3 会接收调用。

我有一个样本 here .

(我目前正在我自己的应用程序中解决这个问题,方法是在每个 fragment 中都有一个 mHandleContext 变量,并在页面更改时启用/禁用它。这样 onContextItemSelected 调用将传递到所有 fragment ,直到调用正确的 fragment 。)

我做错了什么还是支持库的错误?附带说明一下,当我使用 ActionBarSherlock 3.5.1 时没有发生这种情况,它有自己的支持库的分支。

最佳答案

所以这是 Google 做出的某种愚蠢的设计决定,或者是完全没有考虑过的事情。解决此问题的最简单方法是使用这样的 if 语句包装 onContextItemSelected 调用:

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

ActionBarSherlock 3.5 中的兼容性库有这样的 hack。

关于android - ViewPager 中的错误 fragment 接收 onContextItemSelected 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9753213/

相关文章:

android - 自定义支持对话框 fragment 文本颜色错误

android - 支持为自动调整 TextViews 添加的库

android - 使用最新版本的 android 支持库编译项目依赖项

java - boolean 值和 Firebase 数据库

android - 使用 ListView-Adapter 预加载更多 View

android - onStateConflict 无冲突

android - 启动时的运行时权限

android - 支持库从 23.1.1 更新到 23.2.1 后,RecyclerView 适配器中的 notifyDataSetChange() 变慢

android - 禁用用户在 BottomSheet 上拖动

android - 类似android的iOS应用程序未运行或关闭状态时如何处理数据类型fcm通知?