ListView is very slow - Android

I want to show images from the resource directory (all of them are saved in png format) in my ListView , but it is very backward. I am a complete noob, so I need help with asynchronous tasks to remove lags.

List adapter:

  public class MyListAdapter extends ArrayAdapter < SettingsItem > { Context context; List < SettingsItem > items; public MyListAdapter(Context context, int resource, List < SettingsItem > objects) { super(context, resource, objects); this.context = context; items = objects; } @Override public View getView(int position, View itemView, ViewGroup parent) { ViewHolder holder; if (itemView == null) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); itemView = inflater.inflate(R.layout.settings_items, parent, false); holder = new ViewHolder(); holder.imageView = (ImageView) itemView.findViewById(R.id.item_icon); holder.textView = (TextView) itemView.findViewById(R.id.item_text); itemView.setTag(holder); } else { holder = (ViewHolder) itemView.getTag(); } SettingsItem settingsItem = items.get(position); if (settingsItem != null) { holder.textView.setText(settingsItem.getName()); new ImageDownloaderTask(holder.imageView, getContext(), settingsItem.getIcon_id()).execute(); } return itemView; } static class ViewHolder { ImageView imageView; TextView textView; } } 

Row class:

 public class SettingsItem{ private String name; private int icon_id; public SettingsItem(String a, int b){ super(); name=a; icon_id=b; } public String getName() { return name; } public int getIcon_id() { return icon_id; } } 

AsyncTask:

 class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; Context context; int id; public ImageDownloaderTask(ImageView imageView, Context context, int id) { imageViewReference = new WeakReference<ImageView>(imageView); this.context=context; this.id=id; } @Override protected Bitmap doInBackground(String... params) { return BitmapFactory.decodeResource(context.getResources(),id ); } @Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; } if (imageViewReference != null) { ImageView imageView = imageViewReference.get(); if (imageView != null) { if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { imageView.setImageResource(R.drawable.medical_bag); } } } } } 

I really need some help. Thanks in advance.:)

+8
performance android android-listview listview android-asynctask
source share
3 answers

I would recommend you use the Glide library, this will increase the performance of loading images in your application and use. It supports the selection, decoding and display of video streams, images and animated GIF files.

It allows you to scroll through any kind of image list as smooth and fast as possible, but Glide is also effective for almost any occasion when you need to select, resize and display a deleted image.

Glide is a fast and efficient open source media and image management solution for the Android platform that wraps media decoding, memory and disk caching and pooling resources in a simple and easy to use interface.

Adding Glide to your project:

 dependencies { compile 'com.github.bumptech.glide:glide:3.6.0' compile 'com.android.support:support-v4:19.1.0' } 

To use the list from the official page:

 @Override public View getView(int position, View recycled, ViewGroup container) { final ImageView myImageView; if (recycled == null) { myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false); } else { myImageView = (ImageView) recycled; } String url = myUrls.get(position); Glide.with(myFragment) .load(url) .centerCrop() .placeholder(R.drawable.loading_spinner) .crossFade() .into(myImageView); return myImageView; } 

For reference, another project that uses this library for faster image loading and better scrolling: https://github.com/chrisbanes/cheesesquare

+4
source share

To create complex lists in your applications, you can use RecyclerView. Learn more about viewing recycler at https://developer.android.com/training/material/lists-cards.html .

Use image gliding to display images. https://github.com/bumptech/glide . This is a terrific library that does all of the memory management for you. Loading bitmaps in image view is a bit painful.

+3
source share

You can use the cache for faster display. I use picasso http://square.imtqy.com/picasso , it is really useful to easily display images and handle the cache. Hope this helps.

 Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2); 
+2
source share

All Articles