[英]ListView like in universal image loader sample app
我使用通用圖像加載器在列表視圖中顯示拇指圖像。 我已經實現了適配器中的基本步驟,一切正常。 問題是,如果我在列表中有10個項目,我可以看到前5個我向下滾動新圖像顯示但當我向上滾動圖像再次更換正確一次,我想要的是前5個圖像哪個在開始時顯示不應更換。 因此,當我向上和向下滾動時,圖像不會一次又一次地被替換。
就像在uil示例應用程序ListActivity中完成的那樣,在向上滾動時不會替換加載后的圖像。
這是代碼
應用類
public class MyApplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// Create default options which will be used for every
// displayImage(...) call if no options will be passed to this method
DisplayImageOptions displayimageOptions = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().build();
// Create global configuration and initialize ImageLoader with this configuration
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).
defaultDisplayImageOptions(displayimageOptions).build();
ImageLoader.getInstance().init(config);
}
}
適配器
public class CarListAdapter extends BaseAdapter {
private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
private Context context;
private ImageLoader imageLoader;
private ImageLoadingListener animateFirstDisplayListener;
public CarListAdapter(Context context , ArrayList<CarDetail> items , ImageLoadingListener animateFirstDisplayListener) {
super();
this.context = context;
this.items = items;
this.animateFirstDisplayListener = animateFirstDisplayListener;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.d("Inside", "GetView");
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
ViewHolder holder = null;
final CarDetail car = items.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.car_list_row, parent , false);
holder = new ViewHolder();
holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvDailyPriceValue);
holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
holder.tvWeekendPriceValue = (TextView) convertView.findViewById(R.id.tvWeekendPriceValue);
holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
holder.btnBookNow = (Button) convertView.findViewById(R.id.btnBookNow);
holder.btnBookNow.setFocusable(false);
holder.btnBookNow.setFocusableInTouchMode(false);
holder.btnBookNow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context , BookingFormActivity.class);
intent.putExtra("car_name", car.getCarName());
intent.putExtra("min_age", car.getMinimumAge());
context.startActivity(intent);
}
});
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvCarName.setText(car.getCarName());
holder.tvDailyPriceValue.setText(car.getDailyPrice());
holder.tvWeeklyPriceValue.setText(car.getWeeklyPrice());
holder.tvWeekendPriceValue.setText(car.getWeekendPrice());
imageLoader.displayImage(car.getThumbUrl(), holder.imgCar, animateFirstDisplayListener);
return convertView;
}
static class ViewHolder {
TextView tvCarName;
TextView tvDailyPriceValue;
TextView tvWeeklyPriceValue;
TextView tvWeekendPriceValue;
ImageView imgCar;
Button btnBookNow;
}
}
您正在調用init()
兩次,第二次調用會覆蓋您的緩存選項。 刪除此行:
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
使用Lazy Listadapter它是簡單而有效的鏈接https://github.com/thest1/LazyList/blob/master/src/com/fedorvlasov/lazylist/LazyAdapter.java
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.