2

在使用查看器之前,我能够更改列表视图的一个元素(使用 te position == numEmission)的背景,但现在每个 tiem 我滚动彩色项目更改,我什至可以拥有 3 个彩色项目,任何人都可以帮助我.

public class ItemAdapterProgramme extends ArrayAdapter<Programme> {

public ArrayList<Programme> resultats;
public Bitmap bm;
public Context context;
public ImageLoader imageLoader = ImageLoader.getInstance();
public DisplayImageOptions options;
static ViewHolder holder;
public  LayoutInflater mInflater ;
public int numEmission;
public Calendar c;
public int cHour;
Typeface typeface_date, typeface_title;

public ItemAdapterProgramme(Context context, ArrayList<Programme> resultat) {
    super(context, R.layout.feed_view_programme, resultat);
    this.context = context;
    resultats = resultat;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    typeface_date = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Medium.ttf");
    typeface_title = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto/Roboto-Condensed.ttf");
}

public View getView(final int position, View convertView, final ViewGroup parent) {

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.feed_view_programme,parent, false);
        holder = new ViewHolder();
        holder.image = (ImageView) convertView.findViewById(R.id.prog_logo);
        holder.titre = (TextView) convertView.findViewById(R.id.prog_title);
        holder.animator = (TextView) convertView.findViewById(R.id.prog_anim);
        holder.start = (TextView) convertView.findViewById(R.id.prog_start);
        holder.end = (TextView) convertView.findViewById(R.id.prog_end);
        holder.description = (TextView) convertView.findViewById(R.id.prog_description);
        holder.animator.setTypeface(typeface_date);
        holder.titre.setTypeface(typeface_title);
        holder.start.setTypeface(typeface_title);
        holder.end.setTypeface(typeface_title);
        holder.description.setTypeface(typeface_title);
        holder.pb = (ProgressBar) convertView.findViewById(R.id.prog_progressBar1);
        holder.relativeLout_emission = (RelativeLayout)convertView.findViewById(R.id.layout_prog);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.titre.setText(resultats.get(position).getTitle());
    holder.animator.setText(resultats.get(position).getAnimateur());
    holder.start.setText("Début: "+resultats.get(position).getStart());
    holder.end.setText("    Fin: "+resultats.get(position).getEnd());
    holder.description.setText(resultats.get(position).getDescription());




    File cacheDir = StorageUtils.getOwnCacheDirectory(context, "UniversalImageLoader/Cache");

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    .denyCacheImageMultipleSizesInMemory()
    .offOutOfMemoryHandling()
    .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) 
    .discCache(new UnlimitedDiscCache(cacheDir))
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
    .imageDownloader(new URLConnectionImageDownloader(5 * 1000, 20 * 1000)) 
    .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
    .enableLogging()
    .build();

    imageLoader.init(config);

    options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.error)
    .showImageForEmptyUri(R.drawable.error)
    .cacheInMemory()
    .cacheOnDisc()
    .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
    .build();

    imageLoader.displayImage(resultats.get(position).getImageUrl(),  holder.image, options, new ImageLoadingListener() {
        @Override
        public void onLoadingStarted() {
            holder.pb.setVisibility(View.VISIBLE);                  }
        @Override
        public void onLoadingFailed(FailReason failReason) {
            holder.pb.setVisibility(View.GONE);                 
        }
        @Override
        public void onLoadingComplete(Bitmap loadedImage) {
            holder.pb.setVisibility(View.GONE);                 
        }
        @Override
        public void onLoadingCancelled() {
            // Do nothing
        }
    });
    c = Calendar.getInstance();  
    cHour=c.get(Calendar.HOUR_OF_DAY);
    Log.d("Adapter cHour", cHour+"");
    numEmission=findEmission(cHour);
    Log.d("Adapter numEmission", numEmission+"");
    if(position==numEmission){
        holder.relativeLout_emission.setBackgroundColor(context.getResources().getColor(R.color.grey));
    }
    return convertView;
}

public int findEmission(int hour){
    for(int i=0;i < resultats.size()-1;i++){
        String start=resultats.get(i).getStart();
        String end=resultats.get(i).getEnd();
        String[] P0=start.split(":");
        String[] P1=end.split(":");
        start=P0[0];
        end=P1[0];
        int intStart=Integer.parseInt(start);
        int intEnd=Integer.parseInt(end);
        if(intStart<=hour && hour<=intEnd) {
            numEmission=i;
            break;
        }
    }
    return numEmission;
}

static class ViewHolder {
    TextView titre, animator, start, end, description;
    ImageView image;
    ProgressBar pb;
    RelativeLayout relativeLout_emission;
}
4

1 回答 1

7

问题不在于持有人,而在于 convertView。ListView 可以多次使用一个 convertView 来提高性能。因此,如果您为物品上色一次,则每次重复使用时,您都会多出一件上色的物品。您可以每次都移除持有者并为新视图充气,但这样做并不好。

尝试像这样修改您的代码

if (position==numEmission) {
    holder.relativeLout_emission.setBackgroundColor(context.getResources().getColor(R.color.grey));
} else {
    // set default background color
    holder.relativeLout_emission.setBackgroundColor(context.getResources().getColor(R.color.default_list_element_background));
}
于 2012-11-05T12:56:13.300 回答