所以我有一个用于gridview的ArrayAdapter,它在从StringRequest获取图像的位置后填充NetworkImageView。当我滚动网格时,每个项目的图像会在确定正确的图像之前多次切换。
我怀疑问题在于,在StringRequest之后图像被imageLoader填充并且网格项被重用,但我认为NetworkImageView取消了以前的图像请求,这会阻止这种行为。 以下是我正在使用的代码 - 任何帮助将不胜感激。
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = inflater.inflate(R.layout.gridrowlayout, parent, false);
holder = new ViewHolder();
assert view != null;
holder.textView = (TextView) view.findViewById(R.id.label);
holder.networkImageView = (MNetworkImageView) view.findViewById(R.id.icon);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
MyObject myObject = myObjects.get(position);
String url = null;
try {
url = getUrl(myObject);
} catch (EncoderException e) {
e.printStackTrace();
}
holder.networkImageView.setDefaultImageResId(R.drawable.loading);
holder.networkImageView.setErrorImageResId(R.drawable.no_image_available);
holder.textView.setText(myObject.getName() + "\n " + myObject.getYear());
if (MyApplication.getInstance().isNetworkAvailable()) {
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String json) {
try {
Resp resp = gson.fromJson(json, Resp.class);
String tbUrl = resp.getPoster();
holder.networkImageView.setImageUrl(tbUrl, imageLoader);
} catch (JsonSyntaxException e) {
Log.i(ImageAdapter.TAG, "status failed");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
MyApplication.getInstance().addToRequestQueue(stringRequest, ImageAdapter.TAG);
}
return view;
}