【问题标题】:ConstraintLayout beta 5 ImageView scaling issues in RecyclerViewRecyclerView 中的 ConstraintLayout beta 5 ImageView 缩放问题
【发布时间】:2017-07-02 20:59:21
【问题描述】:

自从我更新到com.android.support.constraint:constraint-layout:1.0.0-beta5 后,我在使用ImageView 时观察到奇怪的行为,该ConstraintLayoutRecyclerAdapter 中使用的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="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"
    />

适配器如下所示:

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和适配器初始化如下:

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

演示功能是: 如果true 标头为绿色并显示图像。 如果false 标头为红色并且ImageView 被隐藏。

在 RecyclerView 上下滑动后的结果:

使用beta-5(左)和beta-4(右):

我使用 StaggeredGridLayoutManagerLinearLayoutManager 对 API 24 和 API 19 进行了测试

我知道这看起来更像是针对 Google (AOSP issue) 的问题报告,但除非有人指出我犯的错误或解决方法,否则我会使用该帖子来达到此目的。

【问题讨论】:

    标签: android android-recyclerview beta android-constraintlayout


    【解决方案1】:

    我相信这是 beta5 中的一个错误,wrap_content。我提交了一个错误并与一位从事 ConstraintLayout 的 Google 工程师进行了交谈。他确认了这个错误,复制了它,然后几天后他在master 修复了它,所以我希望它会在下一个版本(未知日期)中修复。

    错误报告位于here

    请注意,截至 2 天前它是 Fixed in master.

    【讨论】:

    • 我认为这些问题是不相关的,尽管相似。 Google 在 20 分钟前已经将这个合并到 master 中。无论如何感谢您的回复
    【解决方案2】:

    该问题已由 Google 在库的 master 分支中解决,并将在下一个版本后正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 2021-03-19
      • 2014-03-05
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2018-11-26
      • 2019-12-23
      相关资源
      最近更新 更多