简体   繁体   中英

Android Lazy loading ViewPager

I'm using a ViewPager with circle indicators for showing pictures.

I want to use the url of some images instead. Actually lazy load some images inside ViewPager with circle indicators. I have to change default image adapter to lazyloading adapter (which gets url of the images). Please help me with this.

The code for image adapter is this:

public class ImageAdapter extends BaseAdapter {

private LayoutInflater mInflater;
private static final int[] ids = { R.drawable.cupcake, R.drawable.donut, R.drawable.eclair, R.drawable.froyo,
        R.drawable.gingerbread, R.drawable.honeycomb, R.drawable.icecream };

public ImageAdapter(Context context) {
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return ids.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.image_item, null);
    }
    ((ImageView) convertView.findViewById(R.id.imgView)).setImageResource(ids[position]);
    return convertView;
}

}

And Main activity uses this code to lunch images:

viewFlow = (ViewFlow) findViewById(R.id.viewflow);
    viewFlow.setAdapter(imgLoader, 5);
    CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
    viewFlow.setFlowIndicator(indic);

use universal image loader for lazy load just in your getView() method in your adapter.

You don't need to re-invent lazy loading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM