CollapsingToolbarLayout 在 Recyclerview 下方留空 space

CollapsingToolbarLayout leaving empty space below Recyclerview

我在使用 CoordinatorLayout 和 RecyclerView 时遇到了这个奇怪的问题。

正如您在图片中看到的,RecyclerView 在其下方留有空白 space,其高度 与为 CollapsingToolbarLayout 的 minHeight 属性声明的完全相同

如果我不声明此属性,linear_navigation LinearLayout 将不会保持固定在屏幕顶部(这是所需的行为)。

我是不是漏了什么?这是布局的代码和一些屏幕截图。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_profile"
        android:layout_width="match_parent"
        android:layout_height="@dimen/profile_header_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:minHeight="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@drawable/bg_welcome"
                android:orientation="vertical"
                app:layout_collapseMode="parallax">

                <com.tourwhiz.pro.widgets.TextViewComfortaaBold
                    android:id="@+id/text_username"
                    style="@style/TextViewTW.Title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/view_size_normal"
                    android:gravity="center"
                    android:text="Juancho"
                    android:textSize="@dimen/text_size_medium" />

                <LinearLayout
                    android:id="@+id/linear_settings"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center">

                    <ImageView
                        android:id="@+id/image_settings"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_settings_white" />

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/image_user"
                        android:layout_width="@dimen/profile_image_radious"
                        android:layout_height="@dimen/profile_image_radious"
                        android:layout_margin="@dimen/view_size_normal"
                        android:src="@drawable/madrid_01"
                        app:civ_border_color="@color/karma_aqua_blue"
                        app:civ_border_width="@dimen/view_size_tiny" />

                    <ImageView
                        android:id="@+id/image_edit"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_mode_edit_white" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/linear_navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:layout_collapseMode="pin"
                android:background="@color/white"
                android:orientation="horizontal">

                <LinearLayout
                    android:id="@+id/linear_posts"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/view_size_normal">

                    <com.tourwhiz.pro.widgets.TextViewLatoRegular
                        style="@style/TextViewTW"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/profile_posts_text"
                        android:textSize="@dimen/text_size_large" />

                    <com.tourwhiz.pro.widgets.TextViewLatoRegular
                        android:id="@+id/text_posts_count"
                        style="@style/TextViewTW"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="@dimen/text_size_large"
                        tools:text="1456" />

                </LinearLayout>

                <View
                    android:layout_width="@dimen/separator_height"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="@dimen/text_size_normal"
                    android:layout_marginTop="@dimen/text_size_normal"
                    android:background="@color/dark_grey" />

                <LinearLayout
                    android:id="@+id/linear_friends"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/view_size_normal">

                    <com.tourwhiz.pro.widgets.TextViewLatoRegular
                        style="@style/TextViewTW"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/profile_friends_text"
                        android:textSize="@dimen/text_size_large" />

                    <com.tourwhiz.pro.widgets.TextViewLatoRegular
                        android:id="@+id/text_friends_count"
                        style="@style/TextViewTW"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="@dimen/text_size_large"
                        tools:text="1" />

                </LinearLayout>

                <View
                    android:layout_width="@dimen/separator_height"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="@dimen/text_size_normal"
                    android:layout_marginTop="@dimen/text_size_normal"
                    android:background="@color/dark_grey" />

                <LinearLayout
                    android:id="@+id/linear_reviews"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="@dimen/view_size_normal">

                    <com.tourwhiz.pro.widgets.TextViewLatoRegular
                        style="@style/TextViewTW"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/profile_reviews_text"
                        android:textSize="@dimen/text_size_large" />

                    <com.tourwhiz.pro.widgets.TextViewLatoRegular
                        android:id="@+id/text_reviews_count"
                        style="@style/TextViewTW"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:textSize="@dimen/text_size_large"
                        tools:text="12" />

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_posts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/item_post" />


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

因为您在 RecyclerView 中设置了 android:layout_gravity="bottom" .

移除 android:layout_gravity="bottom .

@KeLiuYue的回答中建议的修复可能有效。

此外(如果上述修复失败)在许多情况下 Design 的库 存在问题。所以在某些情况下此修复可能不起作用,因为布局的设计小于屏幕高度会导致空白,折叠或展开 <CollapsingToolbarLayout> 时出现一些神秘行为。这个库仍然存在错误,希望有一天可以修复该故障。

我终于找到了适合我的情况的解决方法。我不知道它为什么有效,但它确实有效。如果有人知道为什么这是有效的,或者更好,为什么会这样,请回答问题,我很乐意接受正确的答案。

解决方法是在 RecyclerView 下方放置一个空的 gone 视图。它对我有用!

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_posts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/item_post" />


</android.support.design.widget.CoordinatorLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />
</android.support.design.widget.CoordinatorLayout>