Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Map not scrolling properly in CollapsingToolbarLayout

I have a Google Map in a CollapsingToolbarLayout. The map displays perfectly, although the map camera doesn't work well.

When you try to swipe the map to move to other locations, the animation is not smooth, and usually doesn't register swiping the map because the CoordinatorLayout overrides the Map.

This means the map doesn't scroll when you try to, for example, swipe up on the map. The CoordinatorLayout scrolls instead (NestedScrollView moves up instead of the Map changing location).

Does anyone know how to get around this problem?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:fitsSystemWindows="true"
    android:id="@+id/coordlayout"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="80dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:titleEnabled="true"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            >


            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways"
                android:fitsSystemWindows="true"/> 

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:padding="20dp"
        android:background="@color/colorPrimary">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout"> 
        </LinearLayout>

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



 <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/dots"
        app:backgroundTint="#4fcd1e"
        app:layout_anchor="@id/main.appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:id="@+id/fab" />
</android.support.design.widget.CoordinatorLayout>
like image 501
Nickmccomb Avatar asked Feb 27 '26 16:02

Nickmccomb


1 Answers

A bit too late to reply. Just Add the below mentioned code in your activity onCreate() Method.

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appbar.getLayoutParams();
    AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
    behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
        @Override
        public boolean canDrag(AppBarLayout appBarLayout) {
            return false;
        }
    });
    params.setBehavior(behavior);

Map Scrolling will become proper.

like image 91
Arpit Avatar answered Mar 02 '26 04:03

Arpit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!