ViewPager中的错误片段接收onContextItemSelected调用
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).
我有一个应用程序,在ViewPager中显示一些片段(相同类型),我在上下文菜单项中遇到了一些问题。 (我正在使用支持库)。
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.
当在其中一个片段的上下文菜单中选择了上下文菜单项时,错误的片段正在接收onContextItemSelected事件调用。
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.
例如,如果我在寻呼机中的片段#3上,则位置#2处的片段接收它。如果我滑回到片段#2,则片段#3接收该呼叫。
I've got a sample here.
我在这里有一个样本。
(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.)
(我目前正在我自己的应用程序中解决这个问题,在每个片段中都有一个mHandleContext变量,并在页面更改时启用/禁用它。这样,onContextItemSelected调用将发送到所有片段,直到调用右片段。 )
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.
我做错了什么或这是支持库的错误?作为旁注,当我使用ActionBarSherlock 3.5.1时,这并没有发生,它有自己的支持库分支。
5 个解决方案
#1
33
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:
所以这是Google的某种愚蠢的设计决定,或者刚刚完全没有考虑的事情。解决此问题的最简单方法是使用if语句包装onContextItemSelected调用:
if (getUserVisibleHint()) {
// Handle menu events and return true
} else
return false; // Pass the event to the next fragment
The compatibility library in ActionBarSherlock 3.5 had a hack like this.
ActionBarSherlock 3.5中的兼容性库有这样的黑客攻击。
更多相关文章
- 这些片段在Android编程中很有用
- 点击或长按的Android Listview用户交互设计上下文菜单
- 键盘打开时,带有片段的活动不会调整大小
- Android Asyntask:对上下文使用弱引用以避免设备旋转屏幕
- 嵌套片段不保留其状态
- Fragment中出现java.lang.NullPointerException 空指针 上下文为