Search code examples
androidlistviewimageviewdrawableandroid-lazyloading

List getting slow on scroll while loading images from drawable


I have around 300 images in Drawable and need to show them in a listview, but its getting too slow on scrolling. is there any way to use lazyLoading of offline application.

here's my getView method - if i don't show the image the listview is scrolling fine.

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;
    ViewHolder holder;

    if(convertView==null){

        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        vi = inflater.inflate(R.layout.pos_list_item, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.text = (TextView) vi.findViewById(R.id.textView1);
        holder.text1=(TextView)vi.findViewById(R.id.textView2);
        holder.image=(ImageView)vi.findViewById(R.id.imageView1);


       /************  Set holder with LayoutInflater ************/
        vi.setTag( holder );
    }
    else 
        holder=(ViewHolder)vi.getTag();

    if(data.size()<=0)
    {
        holder.text.setText("No Data");

    }
    else
    {
        /***** Get each Model object from Arraylist ********/
        tempValues=null;
        tempValues = (DataStructureList) data.get( position );

        /************  Set Model values in Holder elements ***********/

         holder.text.setText( tempValues.getPosName());
         holder.text1.setText( tempValues.getCatName());

         int resourceId1 = res.getIdentifier(tempValues.getPosImage()+"1", "drawable",     activity.getPackageName());
         holder.image.setImageResource(resourceId1);

         /******** Set Item Click Listner for LayoutInflater for each row *******/

    }
    return vi;
}'

Solution

  • These can be some of the factors for slow scrolling in your code :

    1. setImageResource() does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.

    2. Size and resolution of your images can also be a cause of slow scrolling, try creating thumbnails images of your large images and use.

    3. try to using pagination instead of loading complete data.