使用视图我可以更改我的列表视图中一个元素(te位置== 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;
}
答案 0 :(得分: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));
}