I'm having a problem using a RecyclerView
within a ConstraintLayout
, and wanted to confirm that I'm not doing anything wrong before declaring it as a bug. Basically I've defined 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>
And I've defined its item which is a TextView
within a ConstraintLayout
(or any other 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>
After using a simple adapter to bind some data to this RecyclerView, the output looks like this:
It seems that it gets it right in the next measure cycle, because scrolling down:
And then scrolling back up fixes the first two items whose widths I assume were recalculated:
The ConstraintLayout
around the item - which is supposed to take up the whole width of the view - instead seems to wrap_content
. If this ConstraintLayout
's height is set to wrap_content
, the issue goes away. If the TextView
is removed, the issue goes away. And finally, if the RecyclerView
is not enclosed within a ConstraintLayout
, the issue goes away.
Here's the adapter:
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;
}
}
And here's 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's a complete example.
As stated in the documentation of ConstraintLayout
, you shouldn't use match_parent
for its children:
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"
The proper constraints for your RecyclerView
should look like this:
<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"/>
You should also constrain the TextView
in the item's layout by setting at least one vertical and one horizontal constraint:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Last but not least, you should probably be using the latest version of ConstraintLayout
:
'com.android.support.constraint:constraint-layout:1.1.2'
instead of 1.0.2.