android – Volley的NetworkImageView没有出现在Spinner中
作者:互联网
我想在微调器的文本旁边显示从网络加载的图像(使用谷歌凌空),但由于某种原因,它不会显示.实际上,通过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