正如您在视频中看到的那样,ListView无法顺利滚动。
就像它总是从URL重新加载图像一样。
我想拥有的是它应该看起来流畅,如此专业。
当图像无效时,ImageView应该是不可见的并且为空。
我正在使用Universal-Image-Loader库。
这是我的代码。
String url = MainActivity.IMAGE_URL + "postid=" + model.get(position).getId();
loader.loadImage(url, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.image.setVisibility(View.VISIBLE);
holder.image.setImageBitmap(loadedImage);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.image.setVisibility(View.GONE);
holder.image.setImageBitmap(null);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
holder.image.setVisibility(View.GONE);
holder.image.setImageBitmap(null);
}
});
这里是完整的代码。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.post_adapter, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.avatar = (ImageView) view.findViewById(R.id.post_avatar);
viewHolder.title = (TextView) view.findViewById(R.id.post_title);
viewHolder.time = (TextView) view.findViewById(R.id.post_time);
viewHolder.text = (TextView) view.findViewById(R.id.post_text);
viewHolder.image = (ImageView) view.findViewById(R.id.post_image);
viewHolder.likes = (TextView) view.findViewById(R.id.post_likes);
viewHolder.comments = (TextView) view.findViewById(R.id.post_comments);
viewHolder.likeImage = (ImageView) view.findViewById(R.id.like_image);
viewHolder.likeText = (TextView) view.findViewById(R.id.like_text);
viewHolder.shareBtn = view.findViewById(R.id.share_btn);
viewHolder.commentBtn = view.findViewById(R.id.comment_btn);
viewHolder.likeBtn = view.findViewById(R.id.like_btn);
view.setTag(viewHolder);
} else {
view = convertView;
}
final ViewHolder holder = (ViewHolder) view.getTag();
final boolean hasLiked = model.get(position).isHasLiked();
if(hasLiked) {
holder.likeImage.setImageResource(R.drawable.ic_action_like_clicked);
holder.likeText.setText(context.getString(R.string.dislike));
holder.likeText.setTextColor(context.getResources().getColor(R.color.color_clicked));
} else {
ColorStateList textColor = holder.title.getTextColors();
holder.likeImage.setImageResource(R.drawable.ic_action_like);
holder.likeText.setText(context.getString(R.string.like));
holder.likeText.setTextColor(textColor);
}
holder.title.setText(model.get(position).getTitle());
ImageLoader loader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.avatar)
.showImageOnFail(R.drawable.avatar).build();
loader.displayImage(model.get(position).getAvatar(), holder.avatar, options);
String time = MainActivity.getTimeAgo(new Date(model.get(position).getTime() * 1000), context);
holder.time.setText(time);
if(model.get(position).getText().isEmpty()) {
holder.text.setVisibility(View.GONE);
} else {
holder.text.setVisibility(View.VISIBLE);
holder.text.setText(model.get(position).getText());
}
// HERE
String url = MainActivity.IMAGE_URL + "postid=" + model.get(position).getId();
loader.loadImage(url, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.image.setVisibility(View.VISIBLE);
holder.image.setImageBitmap(loadedImage);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.image.setVisibility(View.GONE);
holder.image.setImageBitmap(null);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
holder.image.setVisibility(View.GONE);
holder.image.setImageBitmap(null);
}
});
holder.likes.setText(String.valueOf(model.get(position).getLikes()));
String comments = model.get(position).getComments() + " " + context.getString(R.string.comments);
holder.comments.setText(comments);
return view;
}
答案 0 :(得分:0)
您可以使用额外的缓存功能扩展DisplayImageOptions
。
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.avatar)
.showImageOnFail(R.drawable.avatar)
.cacheInMemory(true) // default is false
.cacheOnDisk(true) // default is false
.build();
您还可以更改默认ImageLoaderConfiguration
并更改缓存大小或增加线程优先级(如果您愿意)。请注意,与DisplayImageOptions不同,您只需在应用程序中设置一次此配置。请参阅:ImageLoaderConfiguration
答案 1 :(得分:0)
我在尝试使用Universal ImageLoader,Picasso和Glide库后使用此代码,使用RecyclerView对我来说速度最快。
Uri uri = Uri.fromFile(new File(path));
Picasso.with(mContext)
.load(URL_OF_IMAGE)
.error(R.drawable.blank)
.config(Bitmap.Config.RGB_565)
.resizeDimen(R.dimen.d50dp, R.dimen.d50dp)
.centerCrop()
.into(holder.imageview);
检查此owesome链接,详细显示加载时间和内存的差异, https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en