CoordinatorLayout嵌套AppBarLayout使用的坑

关键代码如下:

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/community_person_app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:elevation="0dp">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/community_person_CollapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <include
                    android:id="@+id/comperson_better_item"
                    layout="@layout/comperson_better_item" />

            </com.google.android.material.appbar.CollapsingToolbarLayout>

            <LinearLayout
                android:id="@+id/ll_tabbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/white">
                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tl_community_person"
                    android:layout_width="match_parent"
                    android:layout_height="48dp"
                    android:background="@color/white"
                    app:tabSelectedTextColor="@color/tab_community_bg"
                    app:tabIndicatorColor="@color/tab_community_bg"
                    app:tabTextColor="#666666"
                    app:tabMinWidth="20dp">
                </com.google.android.material.tabs.TabLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:background="#EBEBEB" />
            </LinearLayout>
        </com.google.android.material.appbar.AppBarLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <androidx.viewpager.widget.ViewPager
                android:id="@+id/vp_comperson"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></androidx.viewpager.widget.ViewPager>
        </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

Fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.CompersonFragment">

    <include
        layout="@layout/comperson_view_empty"
        android:visibility="gone" />

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/ssrl_comperson_recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_comperson"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>

效果:


这里可以看见,只有拖动在AppBarLayout里面的控件上面部分才可以滑动,滑动fragment内内容上面部分没有反应
查看源码发现CoordinatorLayout实现了一个NestedScrollingParent2接口,猜想子view需要有NestedScrollView,于是改动了Frament布局文件

改动如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.CompersonFragment">

    <include
        layout="@layout/comperson_view_empty"
        android:visibility="gone" />

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/ssrl_comperson_recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_comperson"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white" />
        </androidx.core.widget.NestedScrollView>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>

改动后效果如下:


OK!完美解决,这里没有深究为什么会这样,大家感兴趣自己看源码哦,小菜鸟无法解释深层原理O(∩_∩)O~

转载声明:转载请注明出处

你可能感兴趣的:(问题解决)