我有一个带有图像和文本项的网格,问题是图像加载正常,但仅限于滚动。如果我理解正确,当我们滚动时会再次调用getView()
,因此视图会更新。
在滚动网格之前,不会加载图像。
我认为这是因为没有正确通知适配器图像加载。我从更新数据的位置调用notifyDataSetChanged()
。但没有任何反应。
我对网格适配器gridView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
RecordHolder holder = null;
row = convertView;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.seriesTitle = (TextView) convertView
.findViewById(com.watch_free_tv.R.id.seriesNameAS);
holder.seriesDisplayImage = (ImageView) convertView
.findViewById(com.watch_free_tv.R.id.seriesIconAS);
convertView.setTag(holder);
} else {
holder = (RecordHolder) convertView.getTag();
}
SeriesData seriesItem = data.get(position);
holder.seriesTitle.setText(seriesItem.getSeriesTitle());
String imageUrl = null;
imageUrl = seriesItem.getImageURL();
if(seriesDisplayImageMap.get(seriesItem.getSeriesID())==null)
if((imageUrl != null&&(!imageUrl.isEmpty()))) {
seriesDisplayImageMap.put(seriesItem.getSeriesID(), AsyncImageLoader.getInstance().loadDrawable(
imageUrl, imageLazyLoad,this));
}
if (imageUrl!=null&&(!imageUrl.isEmpty()))
if(seriesDisplayImageMap.containsKey(seriesItem.getSeriesID())){
holder.seriesDisplayImage.setImageDrawable(seriesDisplayImageMap.get(seriesItem.getSeriesID()));
}
return convertView;
}
上面代码中的AsyncImageLoader
调用了一个方法,我在图像缓存中检查图像或加载图像并返回到网格
AsynchImageLoader.loadDrawable
方法的实施是:
public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback, AllSeries_GridViewAdapter callingAdapter) {
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
Drawable drawable = softReference.get();
if (drawable != null) {
callingAdapter.notifyDataSetChanged();
return drawable;
}
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
Drawable drawable1 = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable1));
Message message = handler.obtainMessage(0, drawable1);
handler.sendMessage(message);
}
}.start();
return null;
}
我在返回图片前调用notifyDataSetChanged
,这应该再次调用getView
吗?
有人可以指出我遗失或做错了吗?