In my Android project I am showing all images from web service(Url)
to ListView
for this purpose I have used Universal image loader
. After loaded some images in List if I scrolled down then scrolled Up,The already loaded images are reloading again.
DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions
.Builder().cacheOnDisk(true).cacheInMemory(true).considerExifParams(true);
ImageLoader.getInstance().displayImage(imageUri, imageView,
displayImageOptionsBuilder.build());
What I tried
ImageAware imageAware = new ImageViewAware(imageView, false);
ImageLoader.getInstance().displayImage(imageUri, imageAware,displayImageOptionsBuilder.build());
Even used this code it wouldn't make any changes in ListView reloading
Edited
Adapter Class
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final String name = getItem(position);
View view = convertView;
if (view == null) {
view = createView();
ViewHolder viewHolder = new ViewHolder();
viewHolder.image = (ImageView) view.findViewById(R.id.capsule_media_list_item_thumbnail_1);
view.setTag(viewHolder);
}
ImageHelper.initImage(viewHolder.image,
imageUrl,
R.drawable.vx_loading, loadingImageResource,displayOptionsCustomizer);
return view;
}
private final DisplayOptionsCustomizer displayOptionsCustomizer = new DisplayOptionsCustomizer() {
@Override
public void customizeImageOptions(DisplayImageOptions.Builder displayImageOptionsBuilder) {
displayImageOptionsBuilder.displayer(new RoundedBitmapDisplayer(ApplicationUtils
.dipToPixelsRounded(6, getContext()), 0));
}
};
static class ViewHolder {
public ImageView image;
}
ImageHelper.java
public static void initImage(final ImageView imageView, final String imageUri,
final int noImageResource, final int loadingImageResource,
final DisplayOptionsCustomizer displayOptionsCustomizer) {
if (StringUtils.isNotBlank(imageUri)) {
DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions
.Builder().cacheOnDisk(true).cacheInMemory(true).considerExifParams(true);
displayImageOptionsBuilder.showImageOnLoading(loadingImageResource)
.showImageOnFail(noImageResource);
if (displayOptionsCustomizer != null) {
displayOptionsCustomizer.customizeImageOptions(displayImageOptionsBuilder);
}
ImageLoader.getInstance().displayImage(imageUri, imageView,
displayImageOptionsBuilder.build());
} else {
if (noImageResource != 0) {
imageView.setImageResource(noImageResource);
} else {
imageView.setVisibility(View.GONE);
}
}
}
Edited Adapter class as per
Dhir Pratap
answer
public class ImagesListAdapter extends ArrayAdapter<String> {
private List imagesList = new ArrayList<String>();
private Context context;
ImageLoader imageLoader;
DisplayImageOptions options;
public ImagesListAdapter(Context context, List<String> imagesList) {
super(context, -1,imagesList);
this.imagesList = imagesList;
this.context = context;
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisk(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(fallbackImage)
.showImageOnFail(fallbackImage)
.showImageOnLoading(fallbackImage).build();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final String name = getItem(position);
View view = convertView;
ViewHolder viewHolder;
if (view == null) {
view = createView();
viewHolder = new ViewHolder();
viewHolder.image = (ImageView) view.findViewById(R.id.capsule_media_list_item_thumbnail_1);
view.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) view.getTag(viewHolder);
}
/*ImageHelper.initImage(viewHolder.image,
imageUrl,
R.drawable.vx_loading, loadingImageResource,displayOptionsCustomizer);*/
imageLoader.displayImage(imageUrl,viewHolder.image,
options);
return view;
}
/* private final DisplayOptionsCustomizer displayOptionsCustomizer = new DisplayOptionsCustomizer() {
@Override
public void customizeImageOptions(DisplayImageOptions.Builder displayImageOptionsBuilder) {
displayImageOptionsBuilder.displayer(new RoundedBitmapDisplayer(ApplicationUtils
.dipToPixelsRounded(6, getContext()), 0));
}
}; */
static class ViewHolder {
public ImageView image;
}
}