ConstraintLayout 内的 RecyclerView 项目不占据其父级的整个宽度

Item of RecyclerView inside a ConstraintLayout does not occupy the full width of its parent

我在 ConstraintLayout 中使用 RecyclerView 时遇到问题,我想在宣布它为错误之前确认我没有做错任何事情。基本上我在 ConstraintLayout:

中定义了 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.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>

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

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

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

它似乎在下一个测量周期中就正确了,因为向下滚动:

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

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

这是适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewGroup vg;
        public ViewHolder(ViewGroup v) {
            super(v);
            vg = v;
        }
    }

    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        ViewGroup v = (ViewGroup) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.simple_item, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // Do nothing
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

这里是 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.ibsplc.icargo.icoandroid.recyclerviewbugtest"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26'
    compile 'com.android.support:design:26.0.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:recyclerview-v7:26.0.0'
    testCompile 'junit:junit:4.12'
}

Here 是一个完整的例子。

ConstraintLayout 的文档中所述,您不应将 match_parent 用于其子项:

Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent"

您的 RecyclerView 的正确约束应如下所示:

<android.support.v7.widget.RecyclerView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:id="@+id/list"/>

您还应该通过设置至少一个垂直约束和一个水平约束来约束项目布局中的 TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

最后但同样重要的是,您可能应该使用最新版本的 ConstraintLayout'com.android.support.constraint:constraint-layout:1.1.2' 而不是 1.0.2.