Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GoogleMap inside of a Fragment in a ViewPager, keep all touch events in the GoogleMap

As the title states, I have a GoogleMap control (API v2) inside of a fragment that is itself an item in a ViewPager. When I drag on the map, if my first drag motion is on the horizontal axis, the ViewPager gets all of the drag events. If I make my first drag motion on the vertical plane, then all subsequent drags are delegated to the map control. I would like to have the GoogleMap capture all drag events. I added some debugging and I see that when I tap on the map control and when I begin dragging on the vertical plane, no onTouch events even make it to the ViewPager, so it seems that the GoogleMap gets the first chance to handle the event or pass it down. I can't find any methods or listeners on the GoogleMap class to tell it to handle all touch/move/drag events.

like image 479
Rich Avatar asked Jan 15 '13 22:01

Rich


1 Answers

The solution was to create a custom ViewPager with an override of the canScroll method and use that in my xml. The canScroll method is passed a tree of child views recursively and then decides if the View is allowed to handle the horizontal scrolling or if it should be captured by the parent ViewPager

public class MyViewPager extends ViewPager {

    public MyViewPager (Context context) {
        super(context);
    }

    public MyViewPager (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        // Not satisfied with this method of checking...
        // working on a more robust solution
        if(v.getClass().getName().equals("maps.j.b")) {
            return true;
        }
        return super.canScroll(v, checkV, dx, x, y);
    }

}

and in your layout...

<com.myproject.view.MyViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />
like image 195
Rich Avatar answered Sep 30 '22 11:09

Rich