RecyclerView在Fragment中无法显示所有项目

5

问题

在我的Activity中,我初始化了一个包含RecyclerViewFragment。但是我发现它没有显示所有项目。也就是说,它只显示了11个项目,而不是14个项目。此外,最后一个可见项目(第12个)被截断了。

我的实现

Activity包含一个空的FrameLayout,作为Fragment容器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res./android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e0e0e0">

<LinearLayout
    android:id="@+id/layout_footer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/layout_border_white"
    android:paddingBottom="@dimen/footer_padding">

    <include layout="@layout/footer"></include>

</LinearLayout>


<FrameLayout
    android:id="@+id/fragment_start"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/layout_footer"/>

</RelativeLayout>

以下是片段中的RecyclerView:

<android.support.constraint.ConstraintLayout   
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="#e0e0e0"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/layout_header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:padding="@dimen/startpage_padding"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <include layout="@layout/header"></include>

</LinearLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/startpage_margin"
    android:scrollbars="vertical"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/layout_header"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ProgressBar
        android:id="@+id/pb_loading_indicator"
        android:layout_width="@dimen/startpage_progressbar"
        android:layout_height="@dimen/startpage_progressbar"
        android:visibility="invisible"/>

</LinearLayout>


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/tv_error_message_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:padding="@dimen/startpage_text_padding"
        android:text="@string/error_message"
        android:textSize="@dimen/startpage_text"
        android:visibility="invisible"/>
</LinearLayout>

解决方法

我已经尝试在RecyclerView中使用match_parent,并添加了paddingBottom。使用paddingBottom="30dp"可以显示一个更多的元素,但这不是一个好的解决方案。此外,我的Activity正在使用在Manifest中设置的AlertDialog主题。删除它也会显示相同的结果。

任何帮助将不胜感激。


2
android:layout_above="@id/layout_footer" 更改为 android:layout_above="@+id/layout_footer"。另外,为什么要在 RecyclerView 中使用 app:layout_constraintTop_toBottomOf="@+id/layout_header" - Piyush
谢谢您的提示,但是并没有帮助到我。我正在使用app:layout_constraintTop_toBottomOf="@+id/layout_header"来定位,因为RecyclerView位于ConstraintLayout中。 - Deno Agüero
RecyclerView 的父视图是什么? - Ojonugwa Jude Ochalifu
@ojonugwaochalifu,我编辑了我的帖子。 - Deno Agüero
1
你实际上错误地使用了约束布局。 - user8466383
显示剩余2条评论
3个回答

30

我终于解决了问题。问题出在我的ConstraintLayout中的RecyclerView上使用了wrap_content来设置其高度。通过使用0dp来设置高度并添加constraintBottom,可以解决父元素将RecyclerView底部截断的问题。

    <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="@dimen/startpage_margin"
    android:scrollbars="vertical"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/layout_header"/>

4
如果使用滚动布局,我还需要将ScrollView更改为android.support.v4.widget.NestedScrollView。 - Yani2000
1
亲爱的上帝,我也遇到了这个问题,花了几个小时才解决。在项目视图上使用了match parent,当然我看不到其他部分...这是一个新手错误,谢谢! - Mikkel Larsen
4
еҜ№жҲ‘иҖҢиЁҖпјҢдҪҝз”ЁNestedScrollViewе’Ңandroid:layout_height="wrap_content"еҸҜд»Ҙи§ЈеҶій—®йўҳгҖӮ жҲ‘дёҚйңҖиҰҒж”№дёә0dpгҖӮ - Chad Schultz
@Deni Agureo,你好,你能解释一下为什么在RecyclerView中layout_height使用wrap_content无效吗?还是这只是Android的一个无法解释的问题之一? - Eitanos30
三个小时的尝试,终于找到了这个救星! - Tyler Kiser

2
尝试将您的XML更改为以下内容,因为约束布局在使用不正确的方式时会包装空格:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:background="#e0e0e0">

    <LinearLayout
        android:id="@+id/layout_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <include layout="@layout/support_simple_spinner_dropdown_item"/>
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/startpage_margin"
        android:scrollbars="vertical"
        android:layout_below="+id/layout_header"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/pb_loading_indicator"
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_centerInParent="true"
            android:visibility="invisible"/>

        <TextView
            android:id="@+id/tv_error_message_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="Error"
            android:textSize="20sp"
            android:visibility="invisible"/>

    </RelativeLayout>
</LinearLayout>

0

我对高度使用了wrap_content,但是Recyclerview的底部仍然滚动到屏幕底部,以至于最后一个项目不可见。我被迫使用一种巧妙的解决方案,在Recyclerview上添加底部填充,直到最后一个项目可见。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接