gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:00 27 4
gpt4 key购买 nike

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

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

Initial scene

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

Scroll down fully

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

Scroll back up

项目周围的 ConstraintLayout - 它应该占据 View 的整个宽度 - 而不是 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。

关于android - ConstraintLayout 内的 RecyclerView 项目不占据其父级的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51347903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com