ListView控件像通用图像加载器示例应用程序 [英] ListView like in universal image loader sample app

查看:147
本文介绍了ListView控件像通用图像加载器示例应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用通用的图像加载器列表视图中显示的拇指图像。我已经实现了适配器的基本步骤和一切工作。现在的问题是,如果我有10个项目在列表中,我可以看到第5,只要我向下滚动新的图像的显示,但是当我滚动备份图像再次正确更换一次,我想是第5张图片这示在开始不应当更换。所以,当我向后滚动向上和向下的图像不一再更换。

就像是在UIL示例应用程序ListActivity做,一旦加载图像不会被替换时,回卷了起来。

下面是code

应用程序类

 公共类MyApplication的扩展应用{

@覆盖
公共无效的onCreate(){
    // TODO自动生成方法存根
    super.onCreate();

    //创建将被用于每个默认选项
   // displayImage(...)称,如果没有的选项将被传递给此方法
    。DisplayImageOptions displayimageOptions =新DisplayImageOptions.Builder()cacheInMemory()cacheOnDisc()建立()。;


    //创建全局配置和初始化ImageLoader的具有这种构造
    ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(getApplicationContext())。
            defaultDisplayImageOptions(displayimageOptions).build();
    。ImageLoader.getInstance()的init(配置);
}
}
 

适配器

 公共类CarListAdapter扩展了BaseAdapter {

私人的ArrayList< CarDetail>项目=新的ArrayList< CarDetail>();
私人上下文的背景下;
私人ImageLoader的ImageLoader的;
私人ImageLoadingListener animateFirstDisplayListener;



公共CarListAdapter(上下文的背景下,ArrayList的< CarDetail>的项目,ImageLoadingListener animateFirstDisplayListener){
    超();
    this.context =背景;
    this.items =项目;
    this.animateFirstDisplayListener = animateFirstDisplayListener;
    ImageLoader的= ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(上下文));

}

@覆盖
公众诠释getCount将(){
    // TODO自动生成方法存根
    返回items.size();
}

@覆盖
公共对象的getItem(INT位置){
    // TODO自动生成方法存根
    返回items.get(位置);
}

@覆盖
众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    // TODO自动生成方法存根
    Log.d(内部,GetView);

    LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ViewHolder支架=无效;
    最后CarDetail车= items.get(位置);


     如果(convertView == NULL){

            convertView = mInflater.inflate(R.layout.car_list_row,父母,假);
            持有人=新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 =(按钮)convertView.findViewById(R.id.btnBookNow);

            holder.btnBookNow.setFocusable(假);
            holder.btnBookNow.setFocusableInTouchMode(假);

            holder.btnBookNow.setOnClickListener(新OnClickListener(){

                @覆盖
                公共无效的onClick(查看为arg0){
                    // TODO自动生成方法存根
                    意向意图=新的意图(背景下,BookingFormActivity.class);
                    intent.putExtra(car_name,car.getCarName());
                    intent.putExtra(MIN_AGE,car.getMinimumAge());
                    context.startActivity(意向);
                }
            });

            convertView.setTag(保持器);
    }
     其他 {

         支架=(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);


  返回convertView;
 }

 静态类ViewHolder {

            TextView的tvCarName;
            TextView的tvDailyPriceValue;
            TextView的tvWeeklyPriceValue;
            TextView的tvWeekendPriceValue;
            ImageView的imgCar;
            按钮btnBookNow;
        }




}
 

解决方案

正在调用的init()两次,第二次调用将覆盖你的缓存选项。 删除这行:

  imageLoader.init(ImageLoaderConfiguration.createDefault(上下文));
 

I am using universal image loader for displaying thumb images in the list view. I have implemented the basic steps in the adapter and everything is working. The problem is if i have 10 items in the list and i can see first 5 as soon as i scroll down new images are displayed but when i scroll back up the images are replaced again with correct once , what i want is first 5 images which are shown at the starting should not be replaced . So that when i scroll back up and down the images are not replaced again and again .

Like it is done in the uil sample app ListActivity , the images once loaded are not replaced when scroll back up.

Here is the code

Application class

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

Adapter

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




}

解决方案

You are calling init() twice, and the second call overrides your caching options. Remove this line:

imageLoader.init(ImageLoaderConfiguration.createDefault(context));

这篇关于ListView控件像通用图像加载器示例应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆