【问题标题】:RecyclerView is cutting off the last itemRecyclerView 正在切断最后一项
【发布时间】:2015-12-20 22:58:20
【问题描述】:

我有一个片段,里面有一个工具栏和一个 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>

列表中的项目非常简单:

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

我现在位于列表的末尾,但最后一个元素看起来仍然被截断。

我正在使用截至 2015 年 9 月 23 日的最新版本的谷歌库 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
 }

任何帮助都将不胜感激,因为我正在为这个问题发疯:(

解决方案

好的,在将代码清理为基本要素并消除复杂性之后,我发现了问题:它是错误标志和缺失或额外属性的组合。以下内容适用于 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>

简而言之,android:fitsSystemWindows="true" 应该只在 coordinatorLayout、AppBarLayout 和 theCollapsingToolbarLayout(这是我们要在 Android 5.x 上根据屏幕调整的),app:layout_scrollFlags应设置为“scroll|enterAlways|enterAlwaysCollapsed”,并且工具栏的高度应为 actionBar 的高度。最后,最好保持 RecyclerView 尽可能干净,这样您就可以控制每个 line item 的布局间距。

【问题讨论】:

  • 我的是一个简单的约束布局,只有一个工具栏和一个recyclerview。您的解决方案可能不适合我的情况。但是@Rami 解决方案对于报告的问题听起来很通用,因为需要将 appbar 布局的高度调整为底部边距以解决问题。

标签: android android-layout android-recyclerview


【解决方案1】:

尝试将您的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"/>

列表项的截断部分,是AppBarLayout的高度。

【讨论】:

  • 如果是android 5或更高版本的设备,marginBottom是否应该设置为0?
  • 添加边距为我修复了它。谢谢老兄,救了我几个小时!
  • 不知道margin的值怎么办? IE。应该是动态的。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    如果您使用的是约束布局,请确保 layout_height0dp 并且 layout_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"
         />
    

    【讨论】:

      【解决方案4】:

      这对我有用。 将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>
      

      【讨论】:

      • 您确定对宽度和高度执行 0dp 可以解决问题吗?它对我不起作用 分享你的答案
      • 你在使用 ConstraintLayout 吗?
      • 我正在使用约束布局并将高度和宽度设置为 0dp,但它不起作用。我认为您将回收器封闭在任何其他布局中并在那里设置高度宽度?你能分享完整的答案吗?
      • 这是我的问题 0dp
      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 是的,它适用于我的场景,在该场景中,我使用嵌套的 Recyler 视图填充网格中的数据,并且 LL 作为父视图,使我的最后一个视图在其边界内剪切和滚动,而不是使 LL 可滚动。让 RL 作为父级成功了,整个布局现在可以按我的意愿滚动​​了。
        【解决方案7】:

        我有一个包含 RecyclerView 的对话框片段。对于我最初的整体约束布局,上述解决方案均无效,因此我将其更改为线性布局,它可以正常工作。这个布局包含一个工具栏和recyclerview。

        【讨论】:

        • 尝试@Rami 答案(在您的答案之后发布)。可能适合你。
        【解决方案8】:

        设置layout_height="match_parent" 然后添加layout_marginBottom="?attr/actionBarSize"50dp 这将补偿 ToolBar 强制 RecyclerView 向下切断最后一个视图。

        如果您启用了hideOnScroll 工具栏,则另外设置nestedScrollingEnabled="false"

        <androidx.recyclerview.widget.RecyclerView
             android:id="@+id/my_recycler_view"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:layout_marginBottom="?attr/actionBarSize"
             android:nestedScrollingEnabled="false"  />
        

        【讨论】:

          【解决方案9】:

          我遇到了同样的问题,我所做的是创建了一个 LinearLayout 并将我的 RecyclerView 放入其中,它解决了我的问题。我希望这也能解决其他问题。

           <LinearLayout
              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/companyLabelView">
          
              <androidx.recyclerview.widget.RecyclerView
                  android:id="@+id/companiesRecyclerView"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="@dimen/_8dp"
                  android:layout_marginStart="@dimen/_8sdp"
                  android:layout_marginLeft="@dimen/_8sdp"
                  android:layout_marginTop="@dimen/_8sdp"
                  android:layout_marginEnd="@dimen/_8sdp"
                  android:layout_marginRight="@dimen/_8sdp"
                  android:layout_marginBottom="@dimen/_8sdp"
                  android:overScrollMode="never"
                  app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  app:spanCount="3"
                  tools:itemCount="10"
                  tools:listitem="@layout/company_item_view" />
          
          </LinearLayout>
          

          【讨论】:

            【解决方案10】:

            在 ConstraintLayout 中使用 RecyclerView 时,我遇到了同样的问题(甚至从我的列表中丢失了整个 ViewHolder 元素)。正如Sila Siebert 所提到的,将 RecyclerView 的 layout_width 和 layout_height 设置为零就可以了。这有点令人困惑,因为编辑器在 XML 文本显示 0dp 后跳回说“match_constraints”。不要让它迷惑你,它应该可以工作。

            【讨论】:

              【解决方案11】:

              创建一个FrameLayoutRecycerView 放入其中,match_parent 用于widthheight。您可以随意调整framelayout 的大小。

              【讨论】:

                【解决方案12】:

                您缺少底部约束。 您应该添加: app:layout_constraintBottom_toBottomOf="parent"recylerview

                【讨论】:

                • 这与问题有何关系?它根本没有提到 ConstraintLayout
                【解决方案13】:

                确保 layout_heightRelativeLayoutRecyclerView 都有 wrap_content

                【讨论】:

                  【解决方案14】:

                  我遇到了同样的问题,经过大量搜索后,我发现我持有 recycleView 的父视图没有受到适当的约束,因为我使用的是 constraintLayout,

                  我的视图层次结构是这样的

                  constraintLayout->pageViewer with tab->fragment->recyclerView
                  

                  在我的情况下,pageViewer 没有得到适当的约束

                  要解决这个问题,请检查父母是否受到适当的疏远或约束

                  【讨论】:

                    【解决方案15】:

                    我知道我迟到了,但今天我遇到了同样的问题。我有一个ConstrantLayout,一个工具栏,一个recyclerview。所以我用了android:layout_height="match_parent",为了防止recyclerview和topbar重叠,我用了app:layout_constraintTop_toBottomOf="@id/toolbar" android:layout_marginTop="?attr/actionBarSize"

                    所以整个代码看起来像这样:

                    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        xmlns:app="http://schemas.android.com/apk/res-auto"
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:onClick="onClick"
                        android:background="@color/shop_main_fragment_background"
                        >
                        <include layout="@layout/toolbar"/>
                        <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/rootRecyclerView"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical"
                            app:layout_constraintTop_toBottomOf="@id/toolbar"
                            android:layout_marginTop="?attr/actionBarSize"
                            >
                        </androidx.recyclerview.widget.RecyclerView>
                    
                    </androidx.constraintlayout.widget.ConstraintLayout>
                    

                    我希望这会有所帮助。

                    【讨论】:

                      【解决方案16】:

                      我遇到了同样的问题,没有一个答案是有帮助的。

                      我通过将android:minHeight="?actionBarSize" 添加到CollapsingToolbarLayout 解决了这个问题。

                      也许这会对某人有所帮助。

                      【讨论】:

                        【解决方案17】:

                        我有类似的问题。将 android:minHeight="?attr/actionBarSize" 添加到 CollapsingToolbarLayout 有帮助。

                        【讨论】:

                          【解决方案18】:

                          问题是回收视图的底部未正确约束到父视图的底部锚点。为了解决这个问题,在回收视图中将layout_constraintBottom_toBottomOf 设置为等于parent,这会将回收视图的底部限制为父视图的底部,因此视图看起来不会被截断。

                          app:layout_constraintBottom_toBottomOf="parent"
                          

                          【讨论】:

                          • 回答问题时,简要说明您的回答对解决问题的作用。
                          【解决方案19】:

                          将宽度和高度设置为 match_parent 并在底部添加一个边距,并在顶部添加 bar 的值。这使我免于数小时的辛勤工作。

                               <android.support.v7.widget.RecyclerView
                                      android:id="@+id/simpleList"
                                      android:layout_width="match_parent"
                                      android:layout_height="match_parent"
                                      android:layout_marginBottom="App_bar_height
                                      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
                          

                          【讨论】:

                            【解决方案20】:

                            android:paddingBottom ="112dp"

                            不可见区域的高度

                            【讨论】:

                              【解决方案21】:

                              android:paddingBottom ="112dp"

                              不可见区域的高度

                              在您的回收站视图中添加以上内容

                              <androidx.recyclerview.widget.RecyclerView
                                  android:id="@+id/main_recycler"
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content"
                                  android:layout_marginTop="10dp"
                                  app:layout_constraintEnd_toEndOf="parent"
                                  app:layout_constraintStart_toStartOf="parent"
                                  android:paddingBottom="112dp"
                                  app:layout_constraintTop_toBottomOf="@+id/simpleSearchView" />
                              

                              【讨论】:

                                【解决方案22】:

                                在这种情况下,我在 recyclerView 上添加了一个约束,使其不会在屏幕外重叠

                                app:layout_constraintBottom_toBottomOf="parent"
                                

                                【讨论】:

                                  【解决方案23】:

                                  将约束布局(父布局)更改为线性布局解决了我的问题。

                                  【讨论】:

                                    【解决方案24】:

                                    派对已经很晚了,但使用 0dp 表示 recyclerView 的高度。这将设置具有约束的视图。

                                    android:layout_height="0dp"
                                    app:layout_constraintBottom_toBottomOf="parent"
                                    

                                    【讨论】:

                                      【解决方案25】:

                                      使用 layout_height 作为 0dp 进行回收视图,它将解决问题

                                      【讨论】:

                                        【解决方案26】:

                                        将此添加到 RecyclerView 对我有用(如果需要,增加 20dp):

                                        android:layout_marginBottom="20dp"
                                        

                                        【讨论】:

                                          【解决方案27】:

                                          我在使用 ConstraintLayout 布局时遇到了这个问题,所以我所做的就是将我的布局包裹在 LinearLayout 中并猜猜它工作得很好,我希望这可以帮助你们。 谢谢:)

                                          【讨论】:

                                            【解决方案28】:
                                              <RelativeLayout
                                                android:layout_width="match_parent"
                                                android:layout_height="match_parent"
                                                android:layout_marginTop="150dp"
                                              android:layout_marginStart="@dimen/_20"
                                                android:layout_marginEnd="@dimen/_20"
                                                app:layout_constraintTop_toBottomOf="@+id/linearLayout">
                                            
                                                <android.support.v7.widget.RecyclerView
                                                    android:id="@+id/recycler_Bookingagain"
                                                    android:layout_width="match_parent"
                                                    android:layout_height="wrap_content"
                                                    android:layout_marginTop="8dp"
                                                    android:focusableInTouchMode="true"
                                                    app:layout_constraintEnd_toEndOf="parent"
                                                    app:layout_constraintHorizontal_bias="0.0"
                                                    app:layout_constraintStart_toStartOf="parent" />
                                            
                                            </RelativeLayout>
                                            

                                            【讨论】:

                                            • 解释解决问题的方式和原因将真正有助于提高帖子的质量。
                                            【解决方案29】:
                                            <android.support.constraint.ConstraintLayout 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:orientation="vertical"
                                                android:paddingBottom="8dp"
                                                >
                                            
                                            
                                                <android.support.v7.widget.Toolbar
                                                    android:id="@+id/toolbar"
                                                    android:layout_width="0dp"
                                                    android:layout_height="?attr/actionBarSize"
                                                    android:background="?attr/colorPrimary"
                                                    android:contentInsetStart="0dp"
                                                    android:contentInsetLeft="0dp"
                                                    android:contentInsetEnd="0dp"
                                                    android:contentInsetRight="0dp"
                                                    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                                                    app:contentInsetEnd="0dp"
                                                    app:contentInsetLeft="0dp"
                                                    app:contentInsetRight="0dp"
                                                    app:contentInsetStart="0dp"
                                                    app:layout_constraintEnd_toEndOf="parent"
                                                    app:layout_constraintStart_toStartOf="parent"
                                                    app:layout_constraintTop_toTopOf="parent"
                                                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
                                            
                                                <android.support.constraint.ConstraintLayout
                                                    android:id="@+id/main_area"
                                                    android:layout_width="0dp"
                                                    android:layout_height="0dp"
                                                    app:layout_constraintBottom_toBottomOf="parent"
                                                    app:layout_constraintLeft_toLeftOf="parent"
                                                    app:layout_constraintRight_toRightOf="parent"
                                                    app:layout_constraintTop_toBottomOf="@+id/toolbar">
                                            
                                                    <ProgressBar
                                                        android:id="@+id/progressBarGetFromServer"
                                                        style="?android:attr/progressBarStyle"
                                                        android:layout_width="wrap_content"
                                                        android:layout_height="wrap_content"
                                                        android:indeterminateDrawable="@drawable/bg_circular_progress"
                                                        android:visibility="invisible"
                                                        app:layout_constraintBottom_toBottomOf="parent"
                                                        app:layout_constraintEnd_toEndOf="parent"
                                            
                                                        app:layout_constraintStart_toStartOf="parent"
                                                        app:layout_constraintTop_toTopOf="parent" />
                                            
                                                    <android.support.v7.widget.RecyclerView
                                                        android:id="@+id/recyclerViewSecondaryGroup"
                                                        android:layout_width="match_parent"
                                                        android:layout_height="wrap_content"
                                                        android:fillViewport="true"
                                                        app:layout_constraintBottom_toBottomOf="parent"
                                                        app:layout_constraintEnd_toEndOf="parent"
                                                        app:layout_constraintStart_toStartOf="parent"
                                            
                                                        />
                                                </android.support.constraint.ConstraintLayout>
                                            
                                            </android.support.constraint.ConstraintLayout>
                                            

                                            【讨论】:

                                            • 使用两个 ConstraintLayout 并把工具栏放在外面。我希望这对你有帮助
                                            • 如果您为您的答案发送一些解释,它会更有帮助。
                                            • 你使用 ConstraintLayout 作为 root ...首先放置工具栏和另一个 ConstraintLayout .set app:layout_constraintTop_toBottomOf = toolbar。
                                            【解决方案30】:

                                            android:layout_gravity="top" 添加到recyclerview。如果这不起作用添加android:layout_gravity="fill_verticle"

                                            【讨论】:

                                              猜你喜欢
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 2018-09-09
                                              • 2019-11-11
                                              • 2016-02-25
                                              • 1970-01-01
                                              • 2017-09-11
                                              • 1970-01-01
                                              相关资源
                                              最近更新 更多