Search code examples
androidgoogle-mapsandroid-fragmentsandroid-scrollview

Required android.gms.maps.GoogleMap found com.examle.myprojec.WorkAroundFragment


Firstly I know that getMap() method is deprecated,but still to avoid scrollview inside map I used this method:

http://www.londatiga.net/it/programming/android/how-to-make-android-map-scrollable-inside-a-scrollview-layout/

Creating a custom fragment. But still,on that getMap() point, I don't know what to do.

Here is my custom Fragment:

public class WorkaroundMapFragment 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());
        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);
        }
    }
}

Here is my xml code for the mapfragment:

<fragment xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/guideline3"
            tools:context="com.example.mapwithmarker.MapsMarkerActivity"
        class="com.example.jonida.restsapp.customFragment.WorkaroundMapFragment"/>

And this is my code inside onViewCreated,( I am not doing anything inside activities in this project.)

public class OrderDetailsActivityFragment extends Fragment implements OnMapReadyCallback {
    @BindView(R.id.scrollView_order_details)
    ScrollView mScrollView;

    private GoogleMap googleMap;
 @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        googleMap=((WorkaroundMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap //Here is my problem :/ ;
        ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).setListener(new WorkaroundMapFragment.OnTouchListener() {
            @Override
            public void onTouch() {
   mScrollView.requestDisallowInterceptTouchEvent(true);}});
}

I would appreciate any suggestion, thank you.


Solution

  • Required android.gms.maps.GoogleMap found com.examle.myprojec.WorkAroundFragment

    from your error you need to declare your googleMap as WorkaroundMapFragment not as a GoogleMap

    Use this

     private WorkaroundMapFragment  googleMap = (WorkaroundMapFragment) getChildFragmentManager()
                        .findFragmentById(R.id.map);
                googleMap.getMapAsync(this);
    
    
                ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.flysl_map))
                        .setListener(new WorkaroundMapFragment.OnTouchListener() {
                            @Override
                            public void onTouch() {
                                mScrollView.requestDisallowInterceptTouchEvent(true);
                            }
                        });
    

    Instead of this

    googleMap=((WorkaroundMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap //Here is my problem :/ ;
            ((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).setListener(new WorkaroundMapFragment.OnTouchListener() {
                @Override
                public void onTouch() {
       mScrollView.requestDisallowInterceptTouchEvent(true);}});
    

    EDIT

    <fragment xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/map"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/guideline3"
                tools:context="com.example.mapwithmarker.MapsMarkerActivity"
                class="com.example.jonida.restsapp.customFragment.WorkaroundMapFragment"/>