为什么我的ImageView会在图像加载到它之前出现(使用Glide)?

时间:2017-01-12 01:23:46

标签: android imageview android-recyclerview android-glide

当我快速滚动我的RecyclerView时,我注意到在将图像实际加载到ImageView之前出现了空白的ImageView(使用Glide)。如果图像没有成功加载,我宁愿它不显示任何东西而不是空的ImageView容器。我不希望显示空的ImageView,除非它有加载的图像。在加载图像之前看到下面的空ImageView出现在视觉上是不愉快的,即使它在默认淡入之前只有几毫秒来自Glide的开始:

Empty ImageView that I don't want to see

XML(删除了irrelvant代码)

    <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"
        android:visibility="invisible">

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

RecyclerAdapter.java

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


    class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        LinearLayout itemImageLinearLayout;
        ImageView itemImage;


        public ItemHolder(View view) {
            super(view);

            itemImageLinearLayout = (LinearLayout) view.findViewById(R.id.image_holder_layout);
            this.itemImage = (ImageView) view.findViewById(R.id.flyer_item_image);
        }


        void bind(int listIndex) {
            Glide.with(context)
                    .load(items.get(listIndex).getImageUrl())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .into(itemImage);
            itemImageLinearLayout.setVisibility(View.VISIBLE);
        }
    }
}

澄清一下:Glide淡入动画对我来说并不是必需的。如果解决方案更容易没有淡入,我就可以了。

1 个答案:

答案 0 :(得分:1)

尝试以下任何选项

  1. 删除:android:background =&#34; @android:drawable / dialog_holo_light_frame&#34;来自imageView

  2. 尝试为imageView添加占位符。这将在加载实际图像之前添加默认图像

  3. Glide.with(本)             .load(URL)             .placeholder(R.drawable.placeholder)             .into(ImageView的);