小编ind*_*Gov的帖子

ConstraintLayout 内的 RecyclerView 项不占据其父项的全宽

我在使用中的问题RecyclerView中的ConstraintLayout,想确认我没有宣布它作为一个bug之前做错什么。基本上我已经定义了一个RecyclerView里面 a ConstraintLayout

<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.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/list"/>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我已经定义了它的项目,它是TextView一个ConstraintLayout(或任何其他ViewGroup):

<android.support.constraint.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_margin="8dp"
    android:background="@color/colorAccent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

使用一个简单的适配器将一些数据绑定到这个 RecyclerView 后,输出如下所示:

初始场景

似乎它在下一个测量周期中得到了正确的结果,因为向下滚动:

完全向下滚动

然后向上滚动修复前两个项目,我假设它们的宽度被重新计算:

向上滚动

项目ConstraintLayout周围的 - 应该占据视图的整个宽度 - 相反似乎wrap_content. 如果 thisConstraintLayout的高度设置为wrap_content,问题就会消失。如果TextView删除了,问题就会消失。最后,如果RecyclerView没有包含在 a 中ConstraintLayout,问题就会消失。

这是适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-constraintlayout android-wrap-content

5
推荐指数
1
解决办法
3835
查看次数