7

リスト ビューにサム イメージを表示するためにユニバーサル イメージ ローダーを使用しています。アダプターに基本的な手順を実装しましたが、すべてが機能しています。問題は、リストに10個のアイテムがあり、下にスクロールするとすぐに最初の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;
        }




}
4

2 に答える 2

5

2 回呼び出しinit()ており、2 回目の呼び出しでキャッシュ オプションが上書きされます。次の行を削除します。

imageLoader.init(ImageLoaderConfiguration.createDefault(context));
于 2013-06-05T13:30:43.547 に答える
1

以下の簡単で効率的なリンクである Lazy Listadapter を使用して ください https://github.com/thest1/LazyList/blob/master/src/com/fedorvlasov/lazylist/LazyAdapter.java

于 2013-06-05T13:30:33.883 に答える