【发布时间】:2017-07-02 20:59:21
【问题描述】:
自从我更新到com.android.support.constraint:constraint-layout:1.0.0-beta5 后,我在使用ImageView 时观察到奇怪的行为,该ConstraintLayout 在RecyclerAdapter 中使用的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(右):
我使用 StaggeredGridLayoutManager 和 LinearLayoutManager 对 API 24 和 API 19 进行了测试
我知道这看起来更像是针对 Google (AOSP issue) 的问题报告,但除非有人指出我犯的错误或解决方法,否则我会使用该帖子来达到此目的。
【问题讨论】:
标签: android android-recyclerview beta android-constraintlayout