ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – Volley的NetworkImageView没有出现在Spinner中

2019-07-03 13:13:40  阅读:178  来源: 互联网

标签:android android-spinner android-volley networkimageview


我想在微调器的文本旁边显示从网络加载的图像(使用谷歌凌空),但由于某种原因,它不会显示.实际上,通过Android SDK中的一些测量方法,一遍又一遍地调用适配器的getView方法,其中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);

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

标签:android,android-spinner,android-volley,networkimageview
来源: https://codeday.me/bug/20190703/1367066.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有