android-使用ImageLoader库时在GridView中复制项目
作者:互联网
我正在使用GridView显示ImageViews的列表.图像是通过ImageLoader库从Internet加载的.我还实现了一个自定义适配器(扩展了BaseAdapter):
public class ImagePreviewAdapter extends BaseAdapter {
private Context mContext;
private List<LastPost> mPosts = new ArrayList<LastPost>();
private ImageLoader mImageLoader;
private ImageLoadingListener mLoadingListener;
public ImagePreviewAdapter(Context context, List<LastPost> posts, ImageLoadingListener listener) {
this.mContext = context;
this.mPosts = posts;
this.mLoadingListener = listener;
Activity activity = (Activity)mContext;
this.mImageLoader = ((MeanwhileInRussia)activity.getApplication()).getImageLoader();
}
public int getCount() {
return mPosts.size();
}
public LastPost getItem(int position) {
return mPosts.get(position);
}
public long getItemId(int position) {
LastPost post = mPosts.get(position);
return Long.parseLong(post.getId());
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
// if it's not recycled, initialize some attributes
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
String imageLink = mPosts.get(position).getImageurl();
DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil
.getDisplayImageOptions(mContext);
if (mLoadingListener != null) {
mImageLoader
.displayImage(imageLink, imageView, displayImgOptions, mLoadingListener);
} else {
mImageLoader.displayImage(imageLink, imageView, displayImgOptions);
}
} else {
ImageView restoringView = (ImageView)convertView;
imageView = restoringView;
}
return imageView;
}
}
每当系统调用getView()方法时,我都会创建一个新的ImageView对象,并要求ImageLoader将图像放入其中(如果convertView == null,当然).对于网址的第一部分,一切正常.但是我的问题从第二部分开始:GridView中没有新图像,只有第一部分的副本…我已经意识到问题隐藏在convertView == null中:对于所有理论上新的元素,此检查始终等于FALSE !我的代码没有创建新对象并从URL加载图像,而是将一些OLD对象放置到了新位置.有趣的事实:getItemId(position)方法为我的LastPost对象返回正确的ID.这是否意味着我在适配器中只具有用于正确的LastPosts对象的不正确视图?
有什么建议么?谢谢!
解决方法:
尝试这个….
public class ImagePreviewAdapter extends BaseAdapter
{
private Context mContext;
private List<LastPost> mPosts = new ArrayList<LastPost>();
private ImageLoader mImageLoader;
private ImageLoadingListener mLoadingListener;
/**
* Constructor
*
* @param context
* @param posts
* @param listener - using for showing loading progress
*/
public ImagePreviewAdapter(Context context, List<LastPost> posts, ImageLoadingListener listener) {
this.mContext = context;
this.mPosts = posts;
this.mLoadingListener = listener;
Activity activity = (Activity)mContext;
this.mImageLoader = ((MeanwhileInRussia)activity.getApplication()).getImageLoader();
}
public int getCount() {
return mPosts.size();
}
public LastPost getItem(int position) {
return mPosts.get(position);
}
public long getItemId(int position) {
LastPost post = mPosts.get(position);
return Long.parseLong(post.getId());
}
/*
* Create a new ImageView for each item referenced by the Adapter
*/
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
// if it's not recycled, initialize some attributes
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
}
else
{
ImageView restoringView = (ImageView)convertView;
imageView = restoringView;
}
final String imageLink = mPosts.get(position).getImageurl();
final DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil
.getDisplayImageOptions(mContext);
if (mLoadingListener != null)
{
mImageLoader.displayImage(imageLink, imageView,displayImgOptions,mLoadingListener);
}
else
{
mImageLoader.displayImage(imageLink, imageView, displayImgOptions);
}
return imageView;
}
}
标签:android-gridview,universal-image-loader,android 来源: https://codeday.me/bug/20191031/1979345.html