Volley的网络图像视图未显示在Spinner中



我想在微调器的文本旁边显示一个从网络加载的图像(使用谷歌截击),但由于某些原因,它没有显示。事实上,适配器的getView方法被Android SDK中的一些测量方法反复调用,convertView等于null。

以下是我的适配器的样子:

public class CategoriesSpinnerAdapter extends ArrayAdapter<Category> {
private LayoutInflater mLayoutInflater;
public CategoriesSpinnerAdapter(Context context, List<Category> objects) {
    super(context, 0, objects);
    mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.item_spinner_category, parent, false);
    }
    final TextView categoryTitle = (TextView) convertView.findViewById(R.id.scategory_title);
    categoryTitle.setText(getItem(position).getName());
    final NetworkImageView categoryImage = (NetworkImageView) convertView.findViewById(R.id.scategory_image);
    ImageUtils.load(categoryImage, getItem(position).getIcon());
    return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.simple_spinner_category, parent, false);
    }
    final TextView categoryTitle = (TextView) convertView.findViewById(R.id.spinner_item_title);
    categoryTitle.setText(getItem(position).getName());
    final NetworkImageView categoryImage = (NetworkImageView)  convertView.findViewById(R.id.spinner_item_image);
    ImageUtils.load(categoryImage, getItem(position).getIcon());
    return convertView;
}
}

item_spinner_category.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:background="@drawable/top_bar_select_icon" />
<TextView
    android:id="@+id/scategory_title"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"
    android:textColor="@android:color/white" />

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/scategory_image"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="3dp"
    android:layout_centerVertical="true"
    android:src="@drawable/spinner_games_icon"/>
</RelativeLayout>

simple_spinner_category.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_item_title"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:textSize="14sp"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:background="@drawable/bottom_button_selector"/>
<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/spinner_item_image"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="3dp"
    android:layout_centerVertical="true" />
</RelativeLayout>

R.id.scategory_image的静态图像会显示很短一段时间,可能直到Volley下载网络图像。getDropDownView运行良好,图像显示在下拉视图中的文本旁边,但主视图似乎没有出现这种情况。

知道这里会发生什么吗?

今天我遇到了同样的问题。由于没有答案,我想分享我的答案,希望这对某人有帮助。

查看NetworkImageView源代码,我发现"问题"就在这里:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    loadImageIfNecessary(true);
}

因此,正是因为我不想接触这个很棒的框架,我创建了一个SpinnerNetworkImageView(从最初的NewtworkImageView复制并粘贴)来定制这个方法:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    loadImageIfNecessary(false); // <-- look @ 'false' 
}

现在一切都很好。

另一种方法是使用普通的ImageView并在适配器中执行ImageRequest:

Response.ErrorListener errorListener = ...
Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
    @Override
public void onResponse(Bitmap bitmap) {
    holder.picture.setImageBitmap(bitmap);
    }
};
ImageRequest req = new ImageRequest(
    url, listener, width, height, Config.ARGB_8888, errorListener);
requestQueue.add(request);

但我选择了第一个解决方案。

我不明白你为什么要使用ImageUtils.load。我使用截击来加载图像,方法是:

NetworkImageView imageViewIcon = (NetworkImageView) itemView.findViewById(R.id.imageViewIcon);
    if (imageViewIcon != null && get(position).getPhotos() != null && get(position).getPhotos().length > 0) {
        imageViewIcon.setImageUrl(get(position).getPhotos()[0], Application.get().getImageLoader());
    }

imageLoader正在应用

public void onCreate() {
    super.onCreate();
    sInstance = this;
    RequestQueue queue = Volley.newRequestQueue(this);
    mApi = new Test_Api(queue);
    ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
        @Override
        public void putBitmap(String key, Bitmap value) {
            mImageCache.put(key, value);
        }
        @Override
        public Bitmap getBitmap(String key) {
            return mImageCache.get(key);
        }
    };
    mImageLoader = new ImageLoader(queue, imageCache);
}

最新更新