1

我有一个很长的列表视图,我尝试更改特定单元格中的颜色,例如位置 == 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;
4

2 回答 2

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/

于 2012-07-22T07:29:52.520 回答
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 中。

于 2012-07-22T08:02:54.447 回答