RecyclerView 中的 ConstraintLayout beta 5 ImageView 缩放问题
ConstraintLayout beta 5 ImageView scaling issues in RecyclerView
自从我更新到 com.android.support.constraint:constraint-layout:1.0.0-beta5
后,我在使用 ImageView
时观察到奇怪的行为,该 ConstraintLayout
在 RecyclerAdapter
中使用
我用最少的步骤制作了一个演示项目:
该行非常简单:一个 header 和一个图像:
<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
header 为绿色并显示图像。
如果 false
header 为红色并且 ImageView
被隐藏。
在 RecyclerView 上上下滑动后的结果:
使用 beta-5
(左)和 beta-4
(右):
我使用 StaggeredGridLayoutManager
和 LinearLayoutManager
对 API 24 & API 19 进行了测试
我知道这看起来更像是 Google (AOSP issue) 的问题报告,但我会使用 post 来达到这个目的,除非有人指出我犯的错误或解决方法。
我认为这是 beta5 中的一个错误,wrap_content
。我提交了一个错误并与一位从事 ConstraintLayout 的 Google 工程师交谈。他确认了这个错误,重现了它,然后在几天后他在 master
中修复了它,所以我预计它会在 下一个版本 (未知日期)中修复。
错误报告位于 here。
请注意,截至 2 天前,它是 Fixed in master.
。
该问题已在库的 master 分支中 Google 解决,将在下一个版本后正常运行。
自从我更新到 com.android.support.constraint:constraint-layout:1.0.0-beta5
后,我在使用 ImageView
时观察到奇怪的行为,该 ConstraintLayout
在 RecyclerAdapter
我用最少的步骤制作了一个演示项目:
该行非常简单:一个 header 和一个图像:
<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
header 为绿色并显示图像。
如果 false
header 为红色并且 ImageView
被隐藏。
在 RecyclerView 上上下滑动后的结果:
使用 beta-5
(左)和 beta-4
(右):
我使用 StaggeredGridLayoutManager
和 LinearLayoutManager
我知道这看起来更像是 Google (AOSP issue) 的问题报告,但我会使用 post 来达到这个目的,除非有人指出我犯的错误或解决方法。
我认为这是 beta5 中的一个错误,wrap_content
。我提交了一个错误并与一位从事 ConstraintLayout 的 Google 工程师交谈。他确认了这个错误,重现了它,然后在几天后他在 master
中修复了它,所以我预计它会在 下一个版本 (未知日期)中修复。
错误报告位于 here。
请注意,截至 2 天前,它是 Fixed in master.
。
该问题已在库的 master 分支中 Google 解决,将在下一个版本后正常运行。