Since I updated to com.android.support.constraint:constraint-layout:1.0.0-beta5
I'm observing strange behavior when using an ImageView
that scales inside a ConstraintLayout
used in an RecyclerAdapter
I've made a demo project with least steps to reproduce:
The row is pretty simple: a header and an image:
<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="wrap_content"
>
<View
android:id="@+id/header"
android:layout_width="0dp"
android:layout_height="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/header"
/>
The adapter looks like this:
public class RecAdapter extends RecyclerView.Adapter<BooleanViewHolder> {
private List<Boolean> list;
public RecAdapter(ArrayList<Boolean> list) {
this.list = list;
}
@Override
public BooleanViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new BooleanViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false));
}
@Override
public void onBindViewHolder(BooleanViewHolder holder, int position) {
holder.header.setBackgroundColor(list.get(position) ? Color.parseColor("#00FF00") : Color.parseColor("#FF0000"));
holder.image.setImageDrawable(null);
if (list.get(position)) {
holder.image.setVisibility(View.VISIBLE);
holder.image.setImageDrawable(holder.image.getContext().getResources().getDrawable(R.drawable.remove_me));
} else {
holder.image.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return list.size();
}
static class BooleanViewHolder extends RecyclerView.ViewHolder {
private final ImageView image;
private final View header;
BooleanViewHolder(View itemView) {
super(itemView);
header = itemView.findViewById(R.id.header);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
RecyclerView
and adapter are initialized as follows:
recView = (RecyclerView) findViewById(R.id.recView);
// final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recView.setLayoutManager(layoutManager);
ArrayList<Boolean> list = new ArrayList<>();
list.add(true);
list.add(false);
list.add(true);
list.add(false);
list.add(true);
list.add(false);
list.add(true);
list.add(false);
recView.setAdapter(new RecAdapter(list));
The demo functionality is:
if true
header is colored in green and the image is displayed.
if false
header is colored in red and the ImageView
is hidden.
The results after swiping down and up on the RecyclerView:
using beta-5
(left) and beta-4
(right):
I did test with API 24 & API 19 using both StaggeredGridLayoutManager
and LinearLayoutManager
I know this looks more like an issue report for Google (AOSP issue), but I'll use that post for that purpose unless someone points out a mistake I made or a workaround.