RecyclerView正在切断最后一项

Sot*_*ris 32 android android-layout android-recyclerview

我有一个带有工具栏的片段和里面的recyclerView.

我使用虚拟数据填充recyclerView,然后尝试显示它们.出于某种原因,recyclerView的最后一个元素正在被截止.

这是片段的XML:

<?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:background="@color/background_1"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/height_of_app_bar"
        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="@color/primary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/placeholder_rect_header"
                app:layout_collapseMode="parallax"/>

            <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/simpleList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

列表中的项目非常简单:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@color/background_1"
    android:orientation="horizontal"
    android:padding="@dimen/space_for_a_bit_of_air">

    <ImageView
        android:id="@+id/album_cover"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_vertical"
        android:scaleType="fitXY"
        android:src="@drawable/placeholder_album_cover"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="@dimen/space_for_distinguishing_stuff"
        android:orientation="vertical">

        <TextView
            android:id="@+id/album_title"
            style="@style/titleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sample_text_c"/>

        <TextView
            android:id="@+id/album_year"
            style="@style/subtitleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sample_text_a"/>

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我现在在列表的末尾,但最后一个元素仍然看起来是截止的

我现在在列表的末尾,但最后一个元素仍然看起来是截止的.

我使用的是2015-09-23 google libraries的最新版本,23.0.1,(即com.android.support:recyclerview-v7:23.0.1),以及build.gradle中的以下配置:

compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
   minSdkVersion 14
   targetSdkVersion 23
   versionCode 1
   versionName "1.0"

   multiDexEnabled true// Enabling multidex support
 }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,因为我很难解决这个问题:(

好的,在将代码清理到基本要素并消除复杂性之后,我发现了问题:它是错误标志和缺失或额外属性的组合.以下适用于Android 4.x和5.x:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_1"
    android:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:collapsedTitleTextAppearance="@style/Title.collapsed"
            app:contentScrim="@color/primary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@style/Title.Expanded"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/artistic_4"
                app:layout_collapseMode="parallax"/>

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_of_app_bar"
                android:background="@drawable/gradient"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/simpleList"
        style="@style/genericRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:listitem="@layout/item_discography_album"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

简而言之,android:fitsSystemWindows ="true"应该只在coordinatorLayout,AppBarLayout和theCollapsingToolbarLayout(我们希望根据Android 5.x上的屏幕进行调整),app:layout_scrollFlags应该设置to"scroll | enterAlways | enterAlwaysCollapsed",工具栏应该具有高度,actionBar的高度.最后,最好保持RecyclerView尽可能干净,这样您就可以控制每个订单项的布局间距.

Yuv*_*uvi 23

我尝试了大多数可能网站上的所有可用选项,但没有得到解决方案。那么,我想我可以使用底部填充吗?是的,这对我有用。

我把代码分享给你。除了高度、宽度和填充之外,不需要更多属性。

android:paddingBottom="?attr/actionBarSize"

 <android.support.v7.widget.RecyclerView
    android:id="@+id/your id name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="?attr/actionBarSize"
    app:layout_constraintTop_toBottomOf="@+id/your field" />
Run Code Online (Sandbox Code Playgroud)


Ram*_*ami 22

尝试将RecyclerView高度更改为"wrap_content"并将AppBarLayout高度添加为边距底部.

<android.support.v7.widget.RecyclerView
        android:id="@+id/simpleList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/height_of_app_bar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Run Code Online (Sandbox Code Playgroud)

列表项的截止部分是高度AppBarLayout.

  • 当您不知道保证金的价值时怎么办?即应该是动态的。 (2认同)

Har*_*ara 9

您可以尝试以下操作,因为它按预期工作:

<android.support.v7.widget.RecyclerView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/layout_header">
</android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)


Sau*_*kar 9

如果您使用约束布局,请确保正确设置layout_heightis0dplayout_constraintBottom_toBottomOf约束。

  <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView"
     />
Run Code Online (Sandbox Code Playgroud)


Sil*_*ert 5

这对我有用。设置recyclerView的高度和宽度0dp

 <android.support.v7.widget.RecyclerView
    android:id="@+id/rv_materias"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="20dp"
    android:background="@color/secondaryLightColor"
    app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/guideline6"></android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)


jyo*_*ish 5

使用 RelativeLayout 作为 RecycerView 的父级。它对我有用。


小智 5

对于约束布局,您可以将layout_height设置为0dp。它将利用回收器视图占据屏幕上剩余的空间。

  <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleTextView"
     />
Run Code Online (Sandbox Code Playgroud)