Search code examples
javaandroidandroid-fragmentsandroid-scrollviewsupportmapfragment

android - MapFragment inside ScrollView inside Fragment


My goal is to let MapFragment handle touch event instead of ScrollView.

MapFragment is being added programmatically inside of ScrollView which is inside of Fragment.

<ScrollView
    android:id="@+id/scrollview_event"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/button_create"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:id="@+id/linearlayout_event"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_light"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearlayout_where"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/linearlayout_header_event"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textview_where"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="10dp"
                    android:text="@string/where"
                    android:textColor="@color/text_dark"
                    android:textSize="@dimen/text_heading" />

                <View
                    android:id="@+id/view_divider_where"
                    android:layout_width="match_parent"
                    android:layout_height="1px"
                    android:layout_below="@id/textview_where"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="30dp"
                    android:background="@color/bg_dark" />

                <ImageView
                    android:id="@+id/imageview_close_event"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_margin="5dp"
                    android:clickable="true"
                    android:onClick="onClickCloseEvent"
                    android:src="@drawable/close_dark" />
            </RelativeLayout>

            <FrameLayout
                android:id="@+id/framelayout_map"
                android:layout_width="match_parent"
                android:layout_height="180dp"
                android:layout_marginTop="10dp" >

                <FrameLayout
                    android:id="@+id/fragment_map"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <ImageView
                    android:id="@+id/imageview_center_event"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="top"
                    android:layout_margin="6dp"
                    android:background="@color/bg_transparent_light"
                    android:clickable="true"
                    android:padding="6dp"
                    android:src="@drawable/event_pin" />

                <ImageView
                    android:id="@+id/imageview_my_position"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="bottom|left"
                    android:layout_margin="6dp"
                    android:background="@color/bg_transparent_light"
                    android:clickable="true"
                    android:padding="6dp"
                    android:src="@drawable/gps_target" />
            </FrameLayout>

            <TextView
                android:id="@+id/textview_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@color/bg_dark"
                android:padding="6dp"
                android:textColor="@color/text_light"
                android:textSize="@dimen/text_address"
                android:visibility="visible" />
        </LinearLayout>

    //other views and layouts...

    </LinearLayout>
</ScrollView>


    mapFragment = new FragmentMap();
    getChildFragmentManager().beginTransaction().add(R.id.fragment_map, mapFragment, TAG).commit();
    getChildFragmentManager().executePendingTransactions();

FragmentMap looks like this:

    public class FragmentMap extends SupportMapFragment {
        private OnTouchListener mListener;
        private boolean mTouchListener = false;

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


            TouchableWrapper frameLayout = new TouchableWrapper(getActivity());

            frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));

            frameLayout.addView(layout);

            setListener(new OnTouchListener() {

                @Override
                public void onTouch() {
                    //Prevents NullPointerException
                }
            });

            return frameLayout;
        }

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

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

        public boolean getTouch() {
            return mTouchListener;
        }

        public class TouchableWrapper extends FrameLayout {

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

            @Override
            public boolean dispatchTouchEvent(MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mTouchListener = true;

                    break;
                case MotionEvent.ACTION_UP:
                    mTouchListener = false;

                    break;
                }
                mListener.onTouch();

                return super.dispatchTouchEvent(event);
            }
        }
    }

I set listener on a map:

mapFragment.setListener(new FragmentMap.OnTouchListener() {
        @Override
        public void onTouch() {
            scrollView.requestDisallowInterceptTouchEvent(true);
        }
    });

This is supposed to work when map fragment is declared in the XML layout but when I am adding it programmatically, it doesn't.


Solution

  • To enable or disable intercepting event in a parent view, you have Android's onInterceptTouchEvent(MotionEvent ev) method. But it is propagated from the top-down (i.e. the parent view returns true if it consumed the event, false otherwise) . So create a class extending the ScrollView, and override that method:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(checkCoordinateCross(ev, YOUR_TARGET_VIEW_ID)) {
            return false;
        }
        return true;
    }
    private boolean checkCoordinateCross(MotionEvent ev, int resId) {
        View target = findViewById(resId);
        if(target == null) {
            return false;
        }
        if(ev.getX() > target.getX() && ev.getX() < target.getX() + target.getWidth() && ev.getY() > target.getY() && ev.getY() < target.getY() + target.getHeight()) {
            return true;
        }
        return false;
    }
    

    Note that YOUR_TARGET_VIEW_ID has to be the child of the ScrollView or findViewById(...) will return null.