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() で、アイテムの要素のテキストの色を青 (file_name) に設定しました。

このリストビューをスクロールすると、新しい色 (青) が失われます (デフォルトの色にリセットされます)。

なんで?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 に答える