Lahiru Prasanna Lahiru Prasanna - 22 days ago 8
Android Question

Can't drag or zoom google map inside a Listview android

I have added a map Fragment to a

ListView
. It works fine but it can't drag or zoom.

MainActivity.java

mapviewheader = getActivity().getLayoutInflater().inflate(R.layout.view_header, mListView, false);
FrameLayout map_container = (FrameLayout) mapviewheader.findViewById(R.id.map_container);


FragmentTransaction mTransaction = this.getChildFragmentManager().beginTransaction();
mapFragment = new SupportMapFragment().newInstance();

//mapFragment.setOnTouchListener
mTransaction.add(map_container.getId(), mapFragment);
mTransaction.commit();

try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
e.printStackTrace();
}

mListView.setParallaxView(mapviewheader);


view_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="400dp"
/>

</LinearLayout>


I have tried to use
onTouchListener(...)
but it doesn't work for me.

Answer

That's because the ListView is already catching the touch events. You'll have to let your map Fragment catch them. For this you could extends the MapSupportFragment you are using.

public class TouchableGoogleMapFragment extends SupportMapFragment {
    private OnTouchListener mListener;

    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
        View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);

        TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

        // make it invisible
        frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        ((ViewGroup) layout).addView(frameLayout,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        return layout;
    }

    public void setListener(OnTouchListener listener) {
        mListener = listener;
    }

    public interface OnTouchListener {
        public abstract void onTouch();
    }

    public class TouchableWrapper extends FrameLayout {

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

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mListener.onTouch();
                    break;
                case MotionEvent.ACTION_UP:
                    mListener.onTouch();
                    break;
            }
            return super.dispatchTouchEvent(event);
        }
    }
}

And then, in your MainActivity you'll have to requestDisallowInterceptTouchEvent(true) when the user touch the Fragment:

// Intercepting touch events
mapFragment.setListener(new TouchableGoogleMapFragment.OnTouchListener() {
    @Override
    public void onTouch() {
        mScrollView.requestDisallowInterceptTouchEvent(true);
    }
});
Comments