简体   繁体   English

RecyclerView 截断最后一项

[英]RecyclerView cutting off last item

在此处输入图像描述We can see here last item is partially visible.我们可以在这里看到最后一项是部分可见的。 How can i fix this?我怎样才能解决这个问题? layout.xml布局.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 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>

I'm also having the same problem.我也有同样的问题。 In my opinion this happened because you set the AppBarLayout XML attribute android:fitsSystemWindows="true".在我看来,这是因为您设置了 AppBarLayout XML 属性 android:fitsSystemWindows="true"。 To solve this i give the RecyclerView margin bottom equal to action bar size为了解决这个问题,我给 RecyclerView 边距底部等于操作栏大小

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

@Lester was right problem was RecyclerView's wrap_content height. @Lester 是正确的问题是 RecyclerView 的 wrap_content 高度。 But changing match_parent was not working because.但是更改 match_parent 不起作用,因为。 This layout was added to a fragment and that fragment was declared wrap_content.此布局已添加到片段中,并且该片段被声明为 wrap_content。 So I have changed fragment's height and recyclerview's height to match_parent and now problem solved.所以我将片段的高度和recyclerview的高度更改为match_parent,现在问题解决了。

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

I had this problem with a RecyclerView inside a ConstraintLayout , and i changed my recyclerview constriants to "0dp" (match_constraint) and had no further trouble.我在ConstraintLayout中的RecyclerView遇到了这个问题,我将我的 recyclerview 约束更改为“0dp”(match_constraint)并且没有进一步的麻烦。

have a look看一看

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

if you want to use the tools:itemCount="28" you will have to import xmlns:tools="http://schemas.android.com/tools" in your XML file.如果你想使用tools:itemCount="28"你必须在你的XML文件中导入xmlns:tools="http://schemas.android.com/tools"

I tried all the available option from most of possible site but I didn't get the solution.我尝试了大多数可能站点的所有可用选项,但没有得到解决方案。 Then, I think can I use bottom padding?那么,我想我可以使用底部填充吗? And Yes, It's work for me.是的,这对我有用。

I am sharing the code to you.我把代码分享给你。 Nothing more attribute required other than height, width & padding.除了高度、宽度和填充之外,不需要更多属性。

android:paddingBottom="?attr/actionBarSize" 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" />

使用 RelativeLayout 而不是 ConstraintLayout。它对我有用。

I'm showing some solution but didn't work Now I find one easy solution for this cutting off the last item in recycler view我正在展示一些解决方案但没有用现在我找到了一个简单的解决方案来切断回收站视图中的最后一项

Add your recycler view into LinearLaout将您的回收站视图添加到 LinearLaout

After adding recycler view into Linearlayout add these two Attributes into recycler view.将回收器视图添加到 Linearlayout 后,将这两个属性添加到回收器视图中。

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

Now, Your Recyclerview looks like this,现在,您的 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"/>

Easiest but not the best solution.最简单但不是最好的解决方案。 Still works;仍然有效;

Return +1 in the getItemCount() method of your RecyclerView.Adapter implementation and wrap your code in onBindViewHolder method override with a try-catch block.RecyclerView.Adapter实现的getItemCount()方法中返回 +1,并将代码包装在使用try-catch块覆盖的onBindViewHolder方法中。

For others.Disabling nested scrolling in recyclerview also causes this problem in CoordinatorLayout with scrollable toolbar or tablayout.对于其他人。在 recyclerview 中禁用嵌套滚动也会导致 CoordinatorLayout 出现此问题,并带有可滚动的工具栏或 tablayout。 Because when you scroll recyclerview, toolbar or tablayout doesn't scroll.因为当您滚动 recyclerview 时,工具栏或 tablayout 不会滚动。

don't align recyclerview with respect to any view.. simply make it match parent and provide margin from top....this worked for me不要将 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"
            />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM