我在 a 中使用 a 时遇到问题RecyclerView
,ConstraintLayout
并想在将其声明为错误之前确认我没有做错任何事情。基本上我已经定义了 a RecyclerView
inside 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>
我已经定义了它的项目,它是TextView
a ConstraintLayout
(或任何其他ViewGroup
)中的 a :
<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
。如果 thisConstraintLayout
的高度设置为wrap_content
,问题就消失了。如果TextView
删除,问题就会消失。最后,如果RecyclerView
没有包含在 aConstraintLayout
中,问题就消失了。
这是适配器:
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'
}
这是一个完整的例子。