【问题标题】:Volley's NetworkImageView not showing up in SpinnerVolley 的 NetworkImageView 未显示在 Spinner 中
【发布时间】:2013-11-22 09:59:40
【问题描述】:

我想在微调器的文本旁边显示从网络加载的图像(使用 google volley),但由于某种原因它没有显示。事实上,适配器的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 表现良好,图像显示在下拉视图中的文本旁边,但主视图似乎不会发生这种情况。

知道这里会发生什么吗?

【问题讨论】:

  • 您找到解决方案了吗?我目前面临同样的问题。不热衷于将图像移到微调器之外...
  • 不幸的是,我没有:(

标签: android android-spinner android-volley networkimageview


【解决方案1】:

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

查看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);

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

【讨论】:

    【解决方案2】:

    我不明白你为什么使用 ImageUtils.load。我正在使用 volley 以这种方式加载图像:

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

    【讨论】:

    • ImageUtils.load 是我定义的辅助函数,它的作用与您的 2 行代码大致相同。我很确定问题不存在,因为我在项目中的任何地方都在使用它,并且它有效。正如我之前所说,由于某种原因,视图会被一遍又一遍地测量......无论如何,我将图标移到微调器之外,问题就消失了,但我对这个解决方案不太满意。
    • 正如 Edy Bolos 所说,这不是问题
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2015-07-20
    • 1970-01-01
    相关资源
    最近更新 更多