Panning google map in CoordinatorLayout causes recyclerview to scroll in android design support library 23.0.1

时间秒杀一切 提交于 2019-12-03 09:08:09

问题


The map is in a collapsingToolbarLayout which is nested in an appBarLayout. In versions 22.2.0 and 22.2.1 of the android design support library, I could pan around the map independently of the coordinatorLayout but in 23.0.1, if i try to pan across the map in the north/south axis, it causes the recyclerview to scroll up/down. Is this a bug or is there a way to pass the touch events from the appBarLayout to the mapFragment?

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways">

        <fragment
            android:id="@+id/mapFragment"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax" />


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

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

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#44CCFF"
    app:behavior_overlapTop="184dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_margin="16dp"
    android:src="@drawable/ic_list_white_24dp"
    app:backgroundTint="#3366FF"
    app:fabSize="normal" />


回答1:


I faced the same problem and solved it this way.

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



回答2:


The same you asked was reported as a Bug

After that AppBarLayout.Behavior.DragCallback came to existence. This was you can disable or drag events inside container.

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



回答3:


Here's the same solution but in Kotlin:

    val appBarLayout = findViewById<AppBarLayout>(R.id.appBar)
    val params = appBarLayout.layoutParams as CoordinatorLayout.LayoutParams
    val behavior = AppBarLayout.Behavior()
    behavior.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
        override fun canDrag(appBarLayout: AppBarLayout): Boolean {
            return false
        }
    })
    params.behavior = behavior


来源:https://stackoverflow.com/questions/32620364/panning-google-map-in-coordinatorlayout-causes-recyclerview-to-scroll-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!