10

I am having a NestedScrollView as a parent and Fragment containing googleMap as its child. When Ever I scroll downwards or upwards in map the NesetdScrollView is scroll and I am unable to scroll inside the map. Till now I have tried a solution from stack overflow by creating a transparent Image but it didn't worked for me.

XML:

  <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/car_progress"
        android:id="@+id/nested_scroll"
        android:visibility="gone"

        >
............
............
<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/first_section"
                android:visibility="gone"
                android:id="@+id/map_wrapper"
                android:orientation="vertical"
                android:layout_marginTop="10dp"
                >

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/dealer_location"
                    android:textAllCaps="true"
                    android:textColor="@color/colorPrimary"
                    style="@style/BoldStyle"
                    android:layout_marginLeft="@dimen/single_title_dimen"
                    android:layout_marginRight="@dimen/single_title_dimen"
                    android:id="@+id/dealer_head"

                    />

                <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_below="@+id/dealer_head"
                    />
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imagetrans"
                    android:layout_alignTop="@+id/map"
                    android:layout_alignBottom="@+id/map"
                    android:layout_alignEnd="@+id/map"
                    android:layout_alignRight="@+id/map"
                    android:layout_alignLeft="@+id/map"
                    android:layout_alignStart="@+id/map"
                    android:src="@android:color/transparent"/>

            </RelativeLayout>
.....
.....
 </android.support.v4.widget.NestedScrollView>

Code:

transImg.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        nv.requestDisallowInterceptTouchEvent(true);
                        // Disable touch on transparent view
                        return false;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        nv.requestDisallowInterceptTouchEvent(false);
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        nv.requestDisallowInterceptTouchEvent(true);
                        return false;

                    default:
                        return true;
                }
            }
        });
Mehvish Ali
  • 732
  • 2
  • 10
  • 34

1 Answers1

44

Try this... use this custom map fragment with touchable wrapper.

MySupportMapFragment mSupportMapFragment;
mSupportMapFragment = (MySupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);
if(mSupportMapFragment != null)
        mSupportMapFragment.setListener(new MySupportMapFragment.OnTouchListener() {
        @Override
        public void onTouch() {
            scrollView.requestDisallowInterceptTouchEvent(true);
        }
    });

MySupportMapFragment

public class MySupportMapFragment extends SupportMapFragment {

    private OnTouchListener mListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

        View layout = super.onCreateView(inflater, parent, savedInstanceState);

        TouchableWrapper frameLayout = new TouchableWrapper(getActivity());
        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);
        }
    }
}
Ashish John
  • 1,867
  • 2
  • 23
  • 38
  • I could not access getChildFragmentManager() from activity. – Bagus Aji Santoso Apr 03 '17 at 10:38
  • getChildFragmentManager() is only used if you are trying to access a fragment which is inside another fragment. To access a fragement within an Activity, we use getSupportFragmentManager() – Ashish John Apr 04 '17 at 06:02
  • 1
    java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to com.bidhee.bottlesup.mvp.view.customview.MySupportMapFragment – Ram Mandal May 21 '18 at 06:17
  • 2
    Perfect! Nice solution. Working fine. – Nilesh Patel Mar 13 '19 at 05:39
  • @RamMandal In your layout, replace this `com.google.android.gms.maps.SupportMapFragment` with `your.package.name.MySupportMapFragment`. – Jacky Apr 26 '19 at 08:42
  • 1
    And my issue fixed by changing this line of code `android:name="com.google.android.gms.maps.SupportMapFragment"` with this line `android:name="com.development.passengerapp.map.MySupportMapFragment"` from XML file of my Activity inside fragment map tag, thanks @AshishJohn – Abbas Jafari Feb 24 '21 at 12:16
  • 1
    Best solution :) – Arnab Kundu Jun 23 '21 at 05:08