1

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):

enter image description here

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.

Ognian Gloushkov
  • 2,549
  • 1
  • 17
  • 34

2 Answers2

0

I believe this is a bug in beta5, with wrap_content. I filed a bug and talked with a Google Engineer working on ConstraintLayout. He confirmed the bug, reproduced it and then in a couple of days he fixed it in master so I expect it will be fixed in the next release (unknown date).

The bug report is located here.

Notice it's Fixed in master. as of 2 days ago.

Martin Marconcini
  • 23,346
  • 18
  • 98
  • 135
  • 1
    I think that the issues were unrelated, though similar. Google has merged this one to master as well as of 20 minutes ago. Anyways thanks for the reply – Ognian Gloushkov Feb 18 '17 at 18:54
0

The issue has been resolved by Google in the library's master branch and will be working properly after the next release.

Ognian Gloushkov
  • 2,549
  • 1
  • 17
  • 34