如果RecyclerView无法覆盖全屏,那么CoordinatorLayout将不起作用?

时间:2016-02-01 09:33:00

标签: android android-recyclerview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@color/fragment.background">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/fragment.background"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/fragment.background"
                android:orientation="vertical"
                app:layout_scrollFlags="scroll|enterAlways">

                <TextView
                    style="@style/merchantHintText"
                    android:id="@+id/contract_template_title"
                    android:layout_width="match_parent"
                    android:background="@color/white"
                    android:padding="15dp"/>

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:src="@color/fragment.background" />

                <com.tqmall.salestool.merchant.task.UploadImgLayout
                    android:id="@+id/contract_template_imglayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:background="@color/white"
                    android:orientation="vertical"
                    android:padding="15dp" />

                <TextView
                    android:id="@+id/contract_template_info"
                    style="@style/merchantHintText"
                    android:layout_width="match_parent"
                    android:background="@color/white"
                    android:padding="15dp"/>

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:src="@color/fragment.background" />
            </LinearLayout>

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

        <com.tqmall.salestool.view.ListRecyclerView
            android:id="@+id/contract_template_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:paddingBottom="40dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    <TextView
        android:id="@+id/contract_template_sure_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/text.red"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/white"
        android:textSize="20sp" />
</RelativeLayout>

当我的适配器有足够数量的数据(其视图比屏幕高得多)时,AppBarLayout中的LinearLayout将正确折叠。但是,如果数据很少,它就不会崩溃。我阅读了AppBarLayout的源代码,但没有找到代码原因。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

只需将Linear布局放入CollapsingToolbarLayout,就像这样

<?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:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <LinearLayout
             android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            <!-- views -->
            </LinearLayout>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/scrollableView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

试试这个。它应该工作。