如何在列表视图中设置单个文本视图的文本颜色而不是所有每个文本视图?

Ric*_*eng 1 android listview

我在自定义列表视图中获得了文本视图,我知道如何设置文本颜色,以便更改列表中的所有文本视图.但是现在,我只想要一个特定的视图来改变颜色,比如listview中有10个项目,我只想要改变第二个textview颜色,其余的保持不变.任何的想法?非常感谢所有的帮助〜

public class CheckWinNoAdapter extends ArrayAdapter<String> {
private final Context context;
private String[] values;



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) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_draw, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);


    textView1.setText(values[position]);




}
Run Code Online (Sandbox Code Playgroud)

}

Bla*_*elt 6

ListView以避免回收其观点浪费内存.因此,不是在每次getView调用时对新视图进行膨胀,而是 将其分配给前一个参数convertView,然后重复使用它.这convertView是你推动滚动的视图.因此,如果TextView推迟滚动与文本视图相同,可能会发生不同的颜色,您将在文本视图中看到"yourcolor",其中您将exepct defaultcolor.所以这里每次getView设置的需要称为文本颜色.

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


    if (converView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          converView  = inflater.inflate(R.layout.list_draw, parent, false);
    }

    TextView textView1 = (TextView) converView.findViewById(R.id.chk_tv1);

    int color = (position == YOUR_POSITION) ? yourcolor : defaultcolor;
    textView1.setTextColor(color);

    textView1.setText(values[position]);

}
Run Code Online (Sandbox Code Playgroud)