通用图像加载器不适用于 ArrayAdapter 中的相对布局

universal image loader not working for Relative layout in ArrayAdapter

我在 json 响应中有图像路径列表。我提示将图像设置为路径中相对布局的背景。所以我被建议并习惯于 ArrayAdapter 和通用图像加载器来实现 this.i 希望我自己 95% 我已经做到了。但问题是它在我的 ui 中只加载了一张图像,它来自我在 json 响应中的两张图像。

这里我在 ArrayAdapter class 中尝试了 getView() 方法

RelativeLayout layContentOfListView;

// Initialize the Relative layout container
        layContentOfListView = (RelativeLayout)rowView.findViewById(R.id.layContentOfListView);

    // Load image, decode it to Bitmap and return Bitmap to callback
        ImageSize targetSize = new ImageSize(150, 100); // result Bitmap will be fit to this size
        imageLoader.loadImage(fullImagePath, targetSize, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (loadedImage != null) {

                    Drawable d = new BitmapDrawable(context.getResources(), loadedImage);
                    layContentOfListView.setBackgroundDrawable(d);
                }
            }
        });

这是我在每行列表视图中的相对布局内容。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/layContentOfListView"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:orientation="vertical"
    android:background="@drawable/gray_boardered_transparent_shape"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_red"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:id="@+id/layHeader"
        android:layout_marginBottom="10dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lblMerchantName"
            android:layout_weight="1"
            android:text="Merchant Name"
            android:textAlignment="textStart"
            android:textSize="15sp"
            android:textColor="@color/color_white"
            />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/lblRewardPoints"
        android:text="Reward points"
        android:textSize="15sp"
        android:textColor="@color/color_white"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:id="@+id/lblRewardName"
        android:text="Reward Name"
        android:textSize="15sp"
        android:textColor="@color/color_white"
        android:layout_below="@+id/lblRewardPoints"
        android:layout_centerHorizontal="true"
        />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgMemStar"
        android:src="@drawable/icon_mem_star"
        android:layout_below="@+id/lblRewardName"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="40dp"
        android:id="@+id/imgMemHand"
        android:src="@drawable/icon_mem_like"
        android:layout_below="@+id/lblRewardName"
        android:layout_toLeftOf="@+id/imgMemPhone"
        />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgMemPhone"
        android:src="@drawable/icon_mem_phone"
        android:layout_below="@+id/lblRewardName"
        android:layout_centerHorizontal="true"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="40dp"
        android:id="@+id/imgMemQRCode"
        android:src="@drawable/icon_mem_qr"
        android:layout_below="@+id/lblRewardName"
        android:layout_toRightOf="@+id/imgMemPhone"
        />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgMemLocation"
        android:src="@drawable/icon_mem_location"
        android:layout_below="@+id/lblRewardName"
        android:layout_alignParentRight="true"/>


</RelativeLayout>

这是我的输出屏幕

我找不到太长的解决方案。谁能在 this.Thanks.!

上请慈善机构

你应该使用 picasso 库,它是最好的。

在 dependencies 或 Gradle 中添加以下行。

    compile 'com.squareup.picasso:picasso:2.5.2'

并使用以下代码在 ImageView 中加载图像。

    Picasso.with(this)
                .load(pictureURL)
                .into(ImageView);

很简单。有关详细信息,请查看这些链接。 1, 2

我已经测试过了,它工作正常。

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RelativeLayout;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class MainActivity extends AppCompatActivity {
RelativeLayout relativeLayout;
String url = "Image_URL";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    relativeLayout = (RelativeLayout) findViewById(R.id.lay);


    Picasso.with(this)
            .load(url)
            .into(new Target() {
                @Override
                public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
            /* Save the bitmap or do something with it here */

                    //Set it in the ImageView
                    Drawable dr = new BitmapDrawable(bitmap);
                    relativeLayout.setBackground(dr);
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }
            });
}
}

在申请之前在清单中使用它。

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

在依赖项或Gradle中添加以下行。

compile 'com.squareup.picasso:picasso:2.5.2'

我认为它会在图像视图上加载,创建一个 fakeimageview 并将其提供给 uil,然后加载完成,您将位图设置在您的相对布局上