Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

android - Wrong fragment in ViewPager receives onContextItemSelected call

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).

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.

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.

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.)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...