【问题标题】:RecyclerView does not show all items inside FragmentRecyclerView 不显示 Fragment 内的所有项目
【发布时间】:2017-09-01 10:44:34
【问题描述】:

问题

在我的Activity 中,我初始化了一个Fragment,其中包含一个RecyclerView。但我已经认识到它没有显示所有项目。这意味着它只显示 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>

这是 Fragment 中的 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-Theme。删除它显示相同的结果。

任何帮助将不胜感激。

【问题讨论】:

  • android:layout_above="@id/layout_footer"改成android:layout_above="@+id/layout_footer"还有你为什么用app:layout_constraintTop_toBottomOf="@+id/layout_header"换成RecyclerView
  • 感谢您的提示,但这并没有帮助。我使用app:layout_constraintTop_toBottomOf="@+id/layout_header" 进行定位,因为RecyclerViewConstraintLayout 内。
  • RecyclerView的父视图是什么?
  • @ojonugwaochalifu 我编辑了我的帖子。
  • 实际上你使用了不正确的约束布局

标签: android android-fragments android-recyclerview


【解决方案1】:

我终于修好了。问题是我的RecyclerView 里面的ConstraintLayout 中的wrap_content。通过使用wrap_content 作为RecyclerView 的高度,父级将其底部切掉。所以你必须使用 0dp 作为高度和一个约束底部。

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

【讨论】:

  • 我还必须从 ScrollView 更改为 android.support.v4.widget.NestedScrollView(如果使用滚动布局)。
  • 亲爱的上帝,我也有这个问题花了几个小时。在项目视图上有匹配的父级,当然我看不到其余的.. 一个菜鸟的错误,谢谢!
  • 使用 NestedScrollView 和 android:layout_height="wrap_content" 为我工作。我不需要更改为0dp
  • @Deni Agueo,您好,您能解释一下为什么 wrap_content 对 RecyclerView 中的 layout_height 不起作用吗?或者它只是 Android 的疾病之一,没有人可以解释?
  • 花了三个小时想办法解决这个问题,我找到了这个救命稻草!
【解决方案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>

【讨论】:

    【解决方案3】:

    我使用 wrap_content 作为高度,但回收视图的底部仍然滚动到屏幕底部,因此最后一个项目不可见。我求助于使用在 recyclerview 上添加底部填充直到最后一个项目可见的解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多