我有一个很长的列表视图,我试图改变特定单元格中的颜色,例如position == 0,它工作正常。但是当我向下滚动列表时,之前屏幕外的另一个单元格也会发生变化。任何的想法?谢谢你的帮助
public class CheckWinNoAdapter extends ArrayAdapter<String> {
private final Context context;
private String[] values;
TextView tvMain;
public CheckWinNoAdapter(Context context, String[] values) {
// TODO Auto-generated constructor stub
super(context, R.layout.list_draw, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_draw, parent, false);
}
tvMain = (TextView) convertView.findViewById(R.id.chk_main);
tvMain.setText(values[position]);
if(position ==0){
tvMain.setTextColor(Color.BLUE);
}
return convertView;
答案 0 :(得分:2)
您正在通过重复使用convertView
来回收视图,您必须始终在getView()
中设置颜色,例如
if (position == 0) {
tvMain.setTextColor(Color.BLUE);
} else {
// set color back to original color
tvMain.setTextColor(Color.BLACK);
}
请参阅http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
答案 1 :(得分:0)
好的,我现在自己弄清楚,而不是ding if(convertView == null){} 我用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_draw, parent, false);
TextView tvMain = (TextView) rowView.findViewById(R.id.chk_main);
return rowView;
所以不要在rowView中回收视图。