bhekman
bhekman

Reputation: 3277

Mapview Fragment + ViewPager, causing strange interactions

I have spent a ridiculous number of hours trying to get a functioning, responsive mapview fragment working in my viewpager, and now that it is finally working I'm not quite sure why!

Background: I am using ActionBarSherlock for my actionbar, I am using a viewpager to switch between three fragments: a list, an imageview + textview, and a mapview. I have a viewpagerindicator to go with my viewpager. I am using the maps version of the android-support-v4.

What Worked: - Override the viewpager's onInterceptTouchEvent method to return false - Set an onClickListener on the mapview that does nothing.

The mapview would appear, but would not respond before I added the listener, but why did adding an onclicklistener make ALL the gestures responsive?

Upvotes: 1

Views: 857

Answers (1)

Lubos Horacek
Lubos Horacek

Reputation: 1582

Maybe you should start again. In my case, all i had to do was put MapFragment in ViewPager with FragmentPageAdapter. Then created my own ViewPager with overriden method just to make MapFragment usable:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // hacky hack to never intercept map pages events unless its on the
    // very edge of screen - last or first fifth of the view size
    if (((FragmentPagerAdapter) getAdapter()).getItem(getCurrentItem()) instanceof SupportMapFragment) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && event.getX() < (getWidth() - (getWidth() / 5)) && event.getX() > (getWidth() / 5)) {
            // Never allow swiping to switch if not right on the edge
            return false;
        }
    }
    return super.onInterceptTouchEvent(event);
}

Upvotes: 2

Related Questions