我有一个 fragment ,里面有一个工具栏和一个 recyclerView。
我正在用虚拟数据填充 recyclerView,然后尝试显示它们。由于某种原因,recyclerView 的最后一个元素被截断了。
这是 fragment 的 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
高度更改为 "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 - RecyclerView 正在切断最后一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742724/