Android Lazy加载ViewPager

时间:2013-05-16 09:48:54

标签: android android-viewpager lazy-loading carousel android-adapter

我正在使用带有圆圈指示符的ViewPager来显示图片。

我想用一些图片的网址代替。实际上懒得在ViewPager中加载一些带有圆圈指示符的图像。我必须将默认图像适配器更改为延迟加载适配器(获取图像的URL)。请帮帮我。

图像适配器的代码是:

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;
}

}

主要活动使用此代码午餐图像:

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

1 个答案:

答案 0 :(得分:1)

在适配器的getView()方法中使用universal image loader进行延迟加载。

您无需重新发明延迟加载。