【发布时间】:2017-01-11 07:24:26
【问题描述】:
我正在使用 Glide 将 URL 加载到 ImageView 中。我的主要问题是,当我滚动时,图像的大小似乎被弄乱了,这导致了一些失真。我的相关代码如下。
XML(只是与 ImageView 相关的部分):
<LinearLayout
android:id="@+id/image_holder_layout"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:gravity="center">
<ImageView
android:id="@+id/flyer_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@android:drawable/dialog_holo_light_frame" />
</LinearLayout>
回收器适配器:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemHolder> {
private ArrayList<Item> items;
Context context;
public RecyclerAdapter(Context context, ArrayList<Item> items) {
this.items = items;
this.context = context;
}
@Override
public ItemHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
boolean shouldAttachToParentImmediately = false;
View view = LayoutInflater.from(context).inflate(R.layout.list_row_flyer, viewGroup, shouldAttachToParentImmediately);
ItemHolder viewHolder = new ItemHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ItemHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
return items.size();
}
class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView itemImage;
public ItemHolder(View view) {
super(view);
this.itemImage = (ImageView) view.findViewById(R.id.flyer_item_image);
}
void bind(int listIndex) {
Glide.with(context)
.load(items.get(listIndex).getImageUrl())
.placeholder(R.drawable.placeholder_flyer_item_image)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.into(itemImage);
}
}
}
【问题讨论】:
-
尝试删除 .placeholder(R.drawable.placeholder_flyer_item_image) 和 .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) 可能会起作用。
标签: android android-recyclerview android-glide