RecyclerView 切断最后一项

RecyclerView cutting off last item

我们可以在这里看到最后一项是部分可见的。我怎样才能解决这个问题? layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    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="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        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">

            <include
                layout="@layout/header"
                android:fitsSystemWindows="true"
                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/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/grey_background">

    <ImageView
        android:id="@+id/image"
        android:layout_width="@dimen/thumbnail_width"
        android:layout_height="@dimen/thumbnail_height"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/image"
        android:orientation="vertical"
        android:padding="@dimen/participant_left_padding">

        <TextView
            android:id="@+id/participants_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name"
            android:textColor="@android:color/white"/>

        <TextView
            android:id="@+id/total_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="view"
            android:textColor="@android:color/white"/>

        <TextView
            android:id="@+id/ranking"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ranking"
            android:textColor="@android:color/white"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/overflow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_action_overflow"/>
</RelativeLayout>

@Lester 是对的,问题是 RecyclerView 的 wrap_content 高度。但是改变 match_parent 是行不通的,因为。此布局已添加到一个片段,该片段已声明 wrap_content。所以我将 fragment 的高度和 recyclerview 的高度更改为 match_parent,现在问题解决了。

<fragment
        android:id="@+id/fragment"
        android:name="com.example.fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

我也遇到了同样的问题。在我看来,这是因为您设置了 AppBarLayout XML 属性 android:fitsSystemWindows="true"。为了解决这个问题,我给 RecyclerView margin bottom 等于 action bar size

 <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/recyclerView"
        android:layout_marginBottom="?attr/actionBarSize">

我在 ConstraintLayout 中的 RecyclerView 遇到了这个问题,我将我的 recyclerview constriants 更改为“0dp”(match_constraint)并且没有进一步的麻烦。

看看

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/view_with_press_height"
        android:text="@string/taxes_fees_charges"
        android:gravity="center_vertical"
        android:layout_marginStart="@dimen/general_side_margin"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

   <!-- Details Recyclerview-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="@dimen/general_bottom_margin"
        app:layout_constraintVertical_bias="0.0"
        tools:itemCount="28"
        tools:listitem="@layout/tax_details_row" />

</android.support.constraint.ConstraintLayout>

如果您想使用 tools:itemCount="28",您必须在 XML 文件中导入 xmlns:tools="http://schemas.android.com/tools"

使用 RelativeLayout 而不是 ConstraintLayout.It 为我工作。

对于others.Disabling,recyclerview 中的嵌套滚动也会在具有可滚动工具栏或tablayout 的CoordinatorLayout 中导致此问题。因为当你滚动 recyclerview 时,工具栏或 tablayout 不会滚动。

我尝试了大多数可能站点的所有可用选项,但没有找到解决方案。 然后,我想我可以使用底部填充吗?是的,它对我有用。

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

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" />

最简单但不是最好的解决方案。仍然有效;

Return 在 RecyclerView.Adapter 实现的 getItemCount() 方法中 +1 并用 try-catch 块将代码包装在 onBindViewHolder 方法覆盖中。

我展示了一些解决方案,但没有奏效 现在我找到了一个简单的解决方案来解决回收站视图中的最后一项

将您的回收站视图添加到 LinearLaout

在线性布局中添加回收视图后,将这两个属性添加到回收视图中。

android:paddingBottom="?attr/actionBarSize"
android:clipToPadding="false"

现在,您的 Recyclerview 看起来像这样,

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvQuotes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        android:background="@color/white"
        android:paddingBottom="?attr/actionBarSize"
        android:paddingTop="@dimen/_10sdp"
        android:clipChildren="false"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listItem="@layout/item_dessert"
        android:clipToPadding="false"/>

不要将 recyclerview 与任何视图对齐.. 只需使其与父视图匹配并提供距顶部的边距....这对我有用

 <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewAddresses"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            android:layout_marginTop="@dimen/dimen_120dp"
            />