0

This one is my first post on any forum, so apologies for any incorrect format. I am using this custom adapter to get images in listView and it loads those images at once, but I want to load those images one by one scrolling and remove the previous image which is no longer available on the screen after scrolling. thanks for any relevant solution in advance.

class MyAdapter extends BaseAdapter {
    private Context context;
    private int images[];
public MyAdapter(Context context, int images[]) {
    this.context = context;
    this.images = images;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return images.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return images[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

class MyViewHolder {
    ImageView imageView;

    public MyViewHolder(View v) {
        // TODO Auto-generated constructor stub
        imageView = (ImageView) v.findViewById(R.id.imgView);
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    MyViewHolder holder = null;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.custom_list, parent, false);
        holder = new MyViewHolder(row);
        row.setTag(holder);
    } else {
        holder = (MyViewHolder) row.getTag();
    }
    holder.imageView.setImageResource(images[position]);

    return row;
}

}

and here is my MainActivity where I am calling MyAdapter to get that list of images, currently I am calling MyAdapter to get listview of 50 images, but I'll be calling this for 500 images soon.

private ListView listView;
private MyAdapter myAdapter;

 int[] images = { R.drawable.img001, R.drawable.img002, R.drawable.img003,
 R.drawable.img004, R.drawable.img005, R.drawable.img006,
 R.drawable.img007, R.drawable.img008, R.drawable.img009,
 R.drawable.img010, R.drawable.img011, R.drawable.img012,
 R.drawable.img013, R.drawable.img014, R.drawable.img015,
 R.drawable.img016, R.drawable.img017, R.drawable.img018,
 R.drawable.img019, R.drawable.img020, R.drawable.img021,
 R.drawable.img022, R.drawable.img023, R.drawable.img024,
 R.drawable.img025, R.drawable.img026, R.drawable.img027,
 R.drawable.img028, R.drawable.img029, R.drawable.img030,
 R.drawable.img031, R.drawable.img032, R.drawable.img033,
 R.drawable.img034, R.drawable.img035, R.drawable.img036,
 R.drawable.img037, R.drawable.img038, R.drawable.img039,
 R.drawable.img040, R.drawable.img041, R.drawable.img042,
 R.drawable.img043, R.drawable.img044, R.drawable.img045,
 R.drawable.img046, R.drawable.img047, R.drawable.img048,
 R.drawable.img049, R.drawable.img050 };

protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                        myAdapter = new MyAdapter(this, images);
                listView = (ListView) findViewById(R.id.imgList);
                listView.setAdapter(myAdapter);
            }
Faisal Asif
  • 199
  • 2
  • 15
  • Use On ScrollListner See This http://stackoverflow.com/questions/13761639/android-dynamic-loading-list-view-onscrolllistener-issues – Naveen Tamrakar Sep 03 '14 at 11:20

3 Answers3

0

You should not load your images outside the Adapter. Instead load each one on demand using an asynctask per image and also use a cache so images should not be loaded everytime.

Check this link http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

juanmeanwhile
  • 2,594
  • 2
  • 24
  • 26
0

Since you are using your own drawable resources instead of downloading the images from internet, there is no issue. ListView allocates the drawables when the view is created and GC should free the images which are not anymore visible. Because you are holding only int references for the drawables all images are not created when you set the adapter.

Niko
  • 8,093
  • 5
  • 49
  • 85
0

If you are using images from resource, then try to reduce their size below 400kb or even below than that and in png format. Image size plays an important role whole rendering it and set it into Imageview. If the size of images will be large then it will load while scrolling and the application will work slow. Removing of unused cell (Images) of ListView will be taken care by adapter only. You don't have to bother about it.

Suneet Agrawal
  • 269
  • 1
  • 17
  • currently i am using images in jpg format. using images in png format will make any difference? and the size of any image is not greater than 280kb. – Faisal Asif Sep 03 '14 at 12:47
  • Yes it will. Try with png format. If it even wont be able to make your app smother then will reduce the size pragmatically. – Suneet Agrawal Sep 03 '14 at 13:19
  • what if i load smaller images in listview and replace image in onScrollStateChanged method of onScrollListener when scrolling stops. how can i do this? what to write in onScrollStateChanged method? @Naveen Tamrakar can u help on this? – Faisal Asif Sep 04 '14 at 10:19
  • I am not Getting your requirement clearly. Do your want to shift the functionally of an adapter to OnScrollStateChanged Method ? adapter has lot of functionality which you cant achieve through changing images on OnScrollStateChanged. like you cant get the exact object which is being tapped by user. So its really a very bad idea. – Suneet Agrawal Sep 04 '14 at 11:03
  • i only want to replace the image in listview item which is on screen when user stops scrolling. – Faisal Asif Sep 04 '14 at 14:14