[英]RecyclerView is cutting off the last item
我有一個片段,里面有一個工具欄和一個 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 盡可能干凈,以便您可以控制每個行項目的布局間距。
嘗試將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: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" />
這對我有用。 將 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>
您可以嘗試以下操作,因為它按預期工作:
<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>
如果您使用約束布局,請確保layout_height
為0dp
並且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"
/>
使用 RelativeLayout 作為 RecycerView 的父級。它對我有用。
對於約束布局,您可以使用將layout_height 設置為 0dp 。 它將占用 Recycler View 屏幕上剩余的空間。
<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"
/>
我有一個包含 RecyclerView 的對話框片段。 對於我最初的整體約束布局,上述解決方案均無效,因此我將其更改為線性布局,該布局沒有問題。 此布局包含一個工具欄和回收站視圖。
設置layout_height="match_parent"
然后添加layout_marginBottom="?attr/actionBarSize"
50dp
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" />
我遇到了同樣的問題,我所做的是創建了一個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>
創建一個FrameLayout
將RecycerView
放入其中,使其width
和height
均為match_parent
。 您可以根據需要framelayout
大小。
在 ConstraintLayout 中使用 RecyclerView 時,我遇到了同樣的問題(甚至從我的列表中丟失了整個 ViewHolder 元素)。 正如Sila Siebert 所提到的,將 RecyclerView 的 layout_width 和 layout_height 設置為零就可以了。 這有點令人困惑,因為當 XML 文本顯示 0dp 時,編輯器會在一段時間后跳回說“match_constraints”。 不要讓它迷惑你,它應該有效。
確保你有layout_height
作為wrap_content
的RelativeLayout
和RecyclerView
您缺少底部約束。 您應該在recylerview
上添加: app:layout_constraintBottom_toBottomOf="parent"
。
我遇到了同樣的問題,經過大量搜索后,我發現我的父視圖持有 recycleView 沒有正確約束,因為我使用了constraintLayout,
我的視圖層次結構就像
constraintLayout->pageViewer with tab->fragment->recyclerView
在我的情況下 pageViewer 沒有被正確約束
要解決這個問題,請檢查父母是否被適當疏遠或約束的天氣
我知道我遲到了,但今天我遇到了同樣的問題。 我有一個ConstrantLayout
、一個工具欄、一個 recyclerview。 所以我使用了android:layout_height="match_parent"
,為了防止 recyclerview 與app:layout_constraintTop_toBottomOf="@id/toolbar" android:layout_marginTop="?attr/actionBarSize"
重疊,我使用了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>
我希望這是有幫助的。
我面臨着同樣的問題,答案的下界很有幫助。
我通過將android:minHeight="?actionBarSize"
到CollapsingToolbarLayout
解決了這個問題。
也許這會幫助某人。
我有類似的問題。 將android:minHeight="?attr/actionBarSize"
到 CollapsingToolbarLayout 有幫助。
問題是回收視圖的底部沒有正確約束到父視圖的底部錨點。 為了解決這個問題,在回收視圖中設置layout_constraintBottom_toBottomOf
等於parent
,這將限制回收視圖的底部是父視圖的底部,因此視圖看起來不會被切斷。
app:layout_constraintBottom_toBottomOf="parent"
將寬度和高度設置為 match_parent 並在底部添加一個邊距,並在頂部添加欄的值。 這使我免於數小時的辛勤工作。
<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"/>
機器人:paddingBottom =“112dp”
不可見區域的高度 <在我的情況下為 112dp>
機器人:paddingBottom =“112dp”
不可見區域的高度 <在我的情況下為 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" />
在這種情況下,我在 recyclerView 上添加了一個約束,使其不會在屏幕外重疊
app:layout_constraintBottom_toBottomOf="parent"
將約束布局(父布局)更改為線性布局為我解決了這個問題。
聚會已經很晚了,但是使用 0dp 來表示 recyclerView 的高度。 這將設置帶有約束的視圖。
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
使用 layout_height 作為 0dp 用於回收器視圖,它將解決問題
將此添加到 RecyclerView 對我有用(如果需要,增加 20dp):
android:layout_marginBottom="20dp"
我在使用布局
ConstraintLayout<\/code>遇到了這個問題,所以我所做的就是將我的布局包裝在
LinearLayout<\/code>並猜猜它工作得很好,希望這可以幫助你們。
謝謝 :)
"
<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>
<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>
將android:layout_gravity="top"
到recyclerview
。 如果這不起作用,請添加android:layout_gravity="fill_verticle"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.