有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java I在回收器视图布局中使用Glide获得不同大小的图像

我需要从网上下载图片,我用Glide来实现这个目的

下载完图片后,它们会出现在Recycler Vew中。部分图像看起来很好,像这样填充所有单元格

enter image description here

但有些图像表现出奇怪的行为,看起来像这样

enter image description here

据我所知,这样的结果是由于Glade是异步工作的,在adapter Glade中创建的单元格没有映像,adapter为单元格创建默认大小之前。。。过了一段时间,glade出现了图像,但cell已经用错误的大小创建了

但如何修复呢

我需要适配器等待,直到glade完成下载,然后为回收者视图创建单元

我的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:paddingBottom="4dp"
安卓:paddingEnd="8dp"
安卓:paddingStart="8dp"
安卓:paddingTop="4dp"
>

<安卓.support.v7.widget.CardView
  安卓:id="@+id/cvInbox"
  安卓:layout_width="match_parent"
  安卓:layout_height="match_parent"
  app:cardCornerRadius="5dp"
  app:cardElevation="15dp"
  >

<com.balysv.materialripple.MaterialRippleLayout
    安卓:id="@+id/rippleInbox"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="@color/standard_white"
    app:mrl_rippleColor="@color/ntz_background_light_grey"
    app:mrl_rippleOverlay="true"
    >

  <LinearLayout
      安卓:id="@+id/cardMainActivityLinearLayout"
      安卓:layout_width="match_parent"
      安卓:layout_height="match_parent"
      安卓:orientation="vertical"
      >

    <ImageView
        安卓:id="@+id/imageViewMainCard"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        />

    <RelativeLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_weight="2"
        安卓:background="@color/standard_white"
        安卓:orientation="vertical"
        安卓:layout_below="@+id/imageViewMainCard"
        >

      <TextView
          安卓:id="@+id/tvBrandName"
          安卓:text="custom brand"
          安卓:textColor="@color/black_color"
          安卓:layout_width="wrap_content"
          安卓:layout_height="wrap_content"
          安卓:layout_alignParentTop="true"
          安卓:layout_alignParentLeft="true"
          />

      <TextView
          安卓:id="@+id/tvItemName"
          安卓:text="custom Type"
          安卓:textColor="@color/black_color"
          安卓:layout_below="@+id/tvBrandName"
          安卓:layout_width="wrap_content"
          安卓:layout_height="wrap_content"
          />

      <TextView
          安卓:id="@+id/tvPrice"
          安卓:text="custom price"
          安卓:textColor="@color/black_color"
          安卓:layout_below="@+id/tvItemName"
          安卓:layout_width="wrap_content"
          安卓:layout_height="wrap_content"
          />

      <Button
          安卓:id="@+id/bAction"
          安卓:layout_width="wrap_content"
          安卓:layout_height="wrap_content"
          安卓:text="button"
          安卓:layout_alignParentTop="true"
          安卓:layout_alignParentEnd="true"

          />

    </RelativeLayout>
  </LinearLayout>
 </com.balysv.materialripple.MaterialRippleLayout>
 </安卓.support.v7.widget.CardView>
</LinearLayout>

滑入适配器

ImageView iv = currentView.ivMainCard;
        String url = currentCard.getImageUrl();

        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.progress_animation)
                .crossFade()
                .into(iv);

共 (2) 个答案

  1. # 1 楼答案

    在我的例子中,我刚刚将lib从Glide更改为Picasso

    现在我的请求是这样的

    Picasso.with(context)
                    .load(url)
                    .placeholder(R.drawable.progress_animation)
                    .error(R.drawable.image_error_404)
                    .into(iv);
    

    一切都好

  2. # 2 楼答案

    在回收器视图的bindViewHolder中,您需要使用所有零参数添加到imageView布局(0,0,0,0)。在你的情况下,代码如下

            ImageView iv = currentView.ivMainCard;
            iv.layout(0,0,0,0);
    
            String url = currentCard.getImageUrl();
    
            Glide.with(context)
                    .load(url)
                    .placeholder(R.drawable.progress_animation)
                    .crossFade()
                    .into(iv);