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 后,新颜色(蓝色)丢失(重置为默认颜色)
为什么?我是否必须以某种方式更改与 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;
}
最佳答案
是的,你需要更换适配器;准确地说,您必须通过扩展 ArrayAdapter 创建一个自定义的。
特别是您必须覆盖 getView(int position, View convertView, ViewGroup parent)
以将已单击的项目显示为蓝色。
相当简单,this tutorial might help , 否则问。
编辑编辑编辑
尝试这种方式,如果它是你想要的:
public int flag=-1;//CREATE A FLAG TO STORE THE POSITION OF THE ITEM CLICKED
//Other stuff...
//...
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);
flag=position;//WHIT THIS YOU KEEP TRACK OF THE ITEM THAT WAS CLICKED
}
});
//Other stuff...
//...
@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);
//NOW YOU CHECK IF THAT POSTION WAS THE ONE CLICKED, IT SETS THE COLOR BLUE
if(position==flag) textView.setTextColor(Color.BLUE);
textView.setText(f.getName());
fullPath.setText(f.getAbsolutePath());
imageView.setImageResource(
BrowseHandler.getFileIconResourceId(f.getAbsolutePath()));
return rowView;
}
所以基本上你使用:
public int flag=-1;
flag=position;
if(position==flag) textView.setTextColor(Color.BLUE);
关于Android:如何永久修改 ListView 的子项(View)-滚动后颜色丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15112475/