【问题标题】:Recycler view does not show items list when layout_height = "match_constraint" in Constraint layout约束布局中 layout_height = "match_constraint" 时回收站视图不显示项目列表
【发布时间】:2019-03-18 18:29:50
【问题描述】:

我想创建一个 UI,如:,我在约束布局下使用回收器视图。 但问题是,当我设置 recyclerView 的 layout_height="match_constraint" 时,它不显示任何列表,当我设置为 layout_height="wrap_content" 时,列表超出范围且不可滚动,当我将其设置为固定时length 那么它不会垂直填充剩余的屏幕。 它显示如下:

当 layout_height = "match_constraint" 时它什么都不显示:

这是我的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".searchResultListFragment">


    <ImageView
        android:id="@+id/thumbnail_img"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:scaleType="fitXY"
        android:src="@drawable/shopping_mart_transparent"
        app:layout_constraintBottom_toTopOf="@id/search_result_recycler_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_search_result_msg"
        style="@style/search_result_msg_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:text="Attraction Points in Naran"
        app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tv_search_result_count"
        style="@style/search_result_count_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="22dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:text="3 Results"
        app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
        app:layout_constraintEnd_toEndOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/search_result_recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/thumbnail_img" />
</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android xml user-interface android-recyclerview android-constraintlayout


    【解决方案1】:

    这是因为在 RecyclerView xml 的最后一行添加了错误的字符

    改变:

    app:layout_constraintTop_toBottomOf="@+id/thumbnail_img"
    

    到:

    app:layout_constraintTop_toBottomOf="@id/thumbnail_img"
    

    对于其他视图(ImageView 和 2 个 TextViews)也是如此,添加 + 意味着您要添加引用而不是链接到该引用。

    另外,ImageView 中不需要这一行:

    app:layout_constraintBottom_toTopOf="@+id/search_result_recycler_view"
    

    【讨论】:

    • 我改变了它,但它仍然没有显示任何东西,除非我改变 layout_height="fixed_height"
    • 你的代码在我的AndroidStudio中运行良好,你能不能尝试给RecyclerView加个背景色看看有没有问题看看有没有
    • 我也试过了。而且layout_height="match_constraint"时甚至不运行adapter的功能。
    • 当您说 match_constraint 时,您的意思是“0dp”,对吗?无论如何我无法重现您的错误。如果您将背景颜色添加到 RecyclerView 并且您仍然看不到它,我真的不知道还有什么可以帮助您
    • 是的,我的意思是 0dp by match_constraint
    【解决方案2】:

    通过大量搜索,我找不到任何解决此问题的方法。所以我想出了自己的解决方案,用RelativeLayout替换约束布局。虽然这是理想的解决方案,但这就是我解决我面临的问题的方式。

    这里是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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".searchResultListFragment"
        android:orientation="vertical">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_weight="0">
            <ImageView
                android:id="@+id/thumbnail_img"
                android:layout_width="0dp"
                android:layout_height="150dp"
                android:scaleType="fitXY"
                android:src="@drawable/shopping_mart_transparent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/tv_search_result_msg"
                style="@style/search_result_msg_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                android:text="Attraction Points in Naran"
                app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
                app:layout_constraintStart_toStartOf="parent" />
    
            <TextView
                android:id="@+id/tv_search_result_count"
                style="@style/search_result_count_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="22dp"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:text="3 Results"
                app:layout_constraintBottom_toBottomOf="@id/thumbnail_img"
                app:layout_constraintEnd_toEndOf="parent" />
        </android.support.constraint.ConstraintLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/search_result_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      相关资源
      最近更新 更多