1
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;
}

}

4

2 回答 2

2

是的,您需要更换适配器;准确地说,您必须通过扩展 ArrayAdapter 创建一个自定义的。

特别是您必须覆盖getView(int position, View convertView, ViewGroup parent)以将已单击的项目显示为蓝色。

相当简单,本教程可能会有所帮助,否则请询问。

编辑 编辑 编辑

试试这种方式,如果它是你想要的:

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);

于 2013-02-27T12:54:07.270 回答
1

您不应该更改其中的文本颜色-您应该将适配器中的该项目标记为已单击-您如何做到这一点取决于您TextViewonClick

将项目标记为已单击后,您可以通知ListView数据已更改并且它将刷新列表 - 在此期间您Adapter可以检查新的单击状态并ListView适当更改颜色。

于 2013-02-27T12:48:35.537 回答