list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View v = parent.getChildAt(position);
TextView fileName = (TextView) v.findViewById(R.id.file_name);
fileName.setTextColor(Color.BLUE);
}
});
OnItemClick(),我将项目元素的文本颜色设置为蓝色(文件名)。
滚动此列表视图后,新颜色(蓝色)丢失(重置为其默认颜色)
为什么?我是否必须以某种方式更改与 ListView 关联的适配器?
编辑:FileListAdapter(用于填充 ListView)
package com.landa.adapter;
import java.io.File;
public class FileListAdapter extends BaseAdapter {
private final Context context;
private final File[] data;
public FileListAdapter(Context context, File[] values) {
this.context = context;
this.data = 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_row, parent, false);
File f = data[position];
TextView textView = (TextView) rowView.findViewById(R.id.file_name);
TextView fullPath = (TextView) rowView.findViewById(R.id.full_path);
ImageView imageView = (ImageView) rowView.findViewById(R.id.file_image);
textView.setText(f.getName());
fullPath.setText(f.getAbsolutePath());
imageView.setImageResource(
BrowseHandler.getFileIconResourceId(f.getAbsolutePath()));
return rowView;
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
}