android - CoordinatorLayout 带有折叠工具栏,NestedScrollView 和 RecyclerView 底部有空白空间(滚动太远)

标签 android android-recyclerview android-coordinatorlayout android-collapsingtoolbarlayout android-nestedscrollview

正如标题所解释的,我正在使用折叠工具栏和一个 NestedScrollView ,其中包含一个 RecyclerView ,并且每当我在 RecyclerView 中有 5 个项目时 或更少,会显示空白区域(例如,如果我只有 2 个项目,底部的空间会很大,但如果我有 6 个以上项目,则不会有空白区域)。

这是图像和 XML:

LayoutInspector and emulator showing the issue

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:background="@color/backgroundColor"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/episodeDetailsCollapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:title="Lorem ipsum dolor sit amet"
            app:navigationIcon="@drawable/ic_arrow_back_black"
            app:layout_scrollFlags="scroll|exitUntilCollapsed" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:contentDescription="@string/episode_photo"
                        android:id="@+id/photoImageView"
                        android:src="@drawable/ic_image_60dp"
                        android:scaleType="centerCrop"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_gravity="top"
                        android:background="@drawable/fade_to_white_gradient_top"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_gravity="bottom"
                        android:background="@drawable/fade_to_white_gradient"/>
                </FrameLayout>

                <Space
                    android:layout_width="wrap_content"
                    android:layout_height="70dp"/>
            </LinearLayout>

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/actionBarSize"
                android:background="@android:color/transparent"
                android:theme="@style/ToolBarStyle"
                app:navigationIcon="@drawable/ic_arrow_back_black"
                app:layout_collapseMode="pin"
                android:id="@+id/episodeListToolbar" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="0dp"
        android:layout_marginBottom="0dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_gravity="bottom"
            android:layout_height="wrap_content">

            <TextView
                android:layout_marginTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingStart="25dp"
                android:textColor="@color/colorPrimary"
                android:textSize="16sp"
                android:paddingBottom="5dp"
                android:text="@string/item_season_episode_placeholder"
                android:id="@+id/seasonEpisodeTextView"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lorem_ipsum"
                android:paddingStart="25dp"
                android:paddingEnd="25dp"
                android:textSize="15sp"
                android:textColor="@color/textColor"
                android:id="@+id/descriptionTextView"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/comments"
                android:textSize="14sp"
                android:textColor="@color/textColor"
                android:padding="5dp"
                android:layout_margin="20dp"
                android:drawableStart="@drawable/ic_comments"
                android:drawablePadding="8dp"
                android:foreground="?selectableItemBackground"
                android:id="@+id/commentsButton"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/other_episodes"
                android:paddingStart="25dp"
                android:paddingEnd="15dp"
                android:textSize="20sp"
                android:textColor="@color/darkTextColor"
                android:textStyle="bold"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center"
                android:visibility="gone"
                android:id="@+id/emptyNextFiveLinearLayout">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_empty_placeholder"
                    android:layout_marginBottom="10dp"
                    android:contentDescription="@string/empty_placeholder_image"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="@string/empty_message"
                    android:textSize="15sp"/>

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="5dp"
                android:paddingEnd="5dp"
                android:id="@+id/episodeDetailsRecyclerView"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

到目前为止我尝试过的解决方案:

  • android:fitsSystemWindows="true" 添加到 CoordinatorLayout
  • android:fillViewport="true" 添加到 NestedScrollView
  • android:layout_gravity="fill_vertical" 添加到 NestedScrollViewRecyclerView
  • android:overScrollMode="never" 添加到 NestedScrollView

最佳答案

这是预期的行为,因为折叠工具栏需要在使用时完全折叠,因此它会在底部创建额外的空白空间。

关于android - CoordinatorLayout 带有折叠工具栏,NestedScrollView 和 RecyclerView 底部有空白空间(滚动太远),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51696305/

相关文章:

java - 设置 SurfaceView 的背景图像

android - Slack 实时消息传递 websocket

java - 如何在RecyclerView中创建异构布局?

android - Fab in Coordinator layout with bottom padding

android - 工具栏与状态栏重叠

android - 克隆 git 仓库

java - 通过eclipse从gitHub中删除文件夹

java - 单击 "View All"按钮后如何显示 RecyclerView 项目

android - getAdapterPosition() 不是方法

android-layout - RecyclerView SCROLL_STATE_IDLE 被延迟调用