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(),我將項目元素的文本顏色設置爲藍色(file_name)。Android:如何永久修改ListView的子項(View) - 滾動後顏色會丟失
滾動列表視圖此之後,新的顏色(藍色)丟失(它被重置爲默認顏色)
爲什麼?我必須以某種方式更改與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;
}
}
您能否再詳述一下?我有我的自定義適配器,我會發布它。 – Tool 2013-02-27 12:56:46
@Tool好主意讓我們來看看適配器 – 2013-02-27 12:58:14
根據您發佈的教程,我確定了一個問題。也許我不應該一直膨脹視圖,但只有當converView爲空時。 – Tool 2013-02-27 13:02:40