【问题标题】:Item of RecyclerView inside a ConstraintLayout does not occupy the full width of its parentConstraintLayout 中的 RecyclerView 项不占其父项的全宽
【发布时间】:2018-12-23 04:18:33
【问题描述】:

我在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 是一个完整的例子。

【问题讨论】:

    标签: android android-recyclerview android-constraintlayout android-wrap-content


    【解决方案1】:

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

    重要提示:不建议将 MATCH_PARENT 用于包含在 约束布局。类似的行为可以通过使用来定义 MATCH_CONSTRAINT 与对应的左/右或上/下 约束被设置为“父”

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2017-07-18
      • 1970-01-01
      • 2011-11-02
      • 2014-10-15
      相关资源
      最近更新 更多