我想使用以下代码为 ListView 中的项目着色:
holder.txtPosition = (TextView)row.findViewById(R.id.txtPosition);
holder.txtPosition.setTextColor(0xFF00CC00);
.
它工作得很好但是:它也每十个项目着色。例如:我为第一个项目设置颜色,所以每第十个项目也会获得颜色。
如果我向下滚动并再次向上滚动,就会有更多的颜色。
在这张图片中,我只为第一个“用户投票”文本着色,然后非常快速地向下滚动并滚动回顶部。
这是什么魔法?
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class List extends ArrayAdapter<ListItem>{
Context context;
int layoutResourceId;
ListItem data[] = null;
public List(Context context, int layoutResourceId, ListItem[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
ListItem entry = data[position];
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.txtVideos = (TextView)row.findViewById(R.id.txtVideos);
holder.txtPosition = (TextView)row.findViewById(R.id.txtPosition);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtcomments = (TextView)row.findViewById(R.id.txtcomment);
row.setTag(holder);
if (entry.fav){
//row.setBackgroundColor(0xFFfcf6bb);
}
}
else
{
holder = (WeatherHolder)row.getTag();
}
holder.txtTitle.setText(entry.teilnehmer);
holder.txtPosition.setText(entry.position);
int num = Integer.parseInt(entry.videos);
if (num == 0 || num > 1){
holder.txtVideos.setText(entry.videos + " Videos");
} else {
holder.txtVideos.setText(entry.videos + " Video");
}
if (holder.txtPosition.getText().equals("Ausgeschieden")){
holder.txtPosition.setTextColor(0xFFFF0000);
} else if(holder.txtPosition.getText().equals("User-Voting")){
holder.txtPosition.setTextColor(0xFF00CC00);
}
num = Integer.parseInt(entry.comment);
if (num == 0 || num > 1){
holder.txtcomments.setText(entry.comment + " Kommentare");
} else {
holder.txtcomments.setText(entry.comment + " Kommentar");
}
return row;
}
static class WeatherHolder
{
TextView txtTitle;
TextView txtVideos;
TextView txtPosition;
TextView txtcomments;
}
}