2

スクロール中にリストビュー項目の奇妙な選択に直面しています。

初期選択スクリーンショット(選択された最初のエントリ) ここに画像の説明を入力

リストビュー項目をスクロールした後、なぜ自動的に選択されるのですか? (下のスクリーンショットを参照)

ここに画像の説明を入力

アダプターのソースコードはこちら

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

setonitemclicklistner() でボタンを表示すると問題が発生する

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
view.setSelected(true);
view.setBackgroundResource(R.drawable.gradient_bg_hover);   
TextView title;
TextView artist;
title = (TextView)view.findViewById(R.id.title); // title
artist = (TextView)view.findViewById(R.id.artist); // artist
title.setTextColor(getResources().getColor(android.R.color.white));
artist.setTextColor(getResources().getColor(android.R.color.white));

ImageButton btnChild = (ImageButton)view.findViewById(R.id.arrow);
btnChild.setVisibility(View.VISIBLE);

if(lastselected!= null)
{

    title = (TextView)lastselected.findViewById(R.id.title); // title
    artist = (TextView)lastselected.findViewById(R.id.artist); // artist
    title.setTextColor(getResources().getColor(android.R.color.black));
    artist.setTextColor(getResources().getColor(android.R.color.black));

     btnChild = (ImageButton)lastselected.findViewById(R.id.arrow);
    btnChild.setVisibility(View.INVISIBLE);
    lastselected.setBackgroundResource(R.drawable.gradient_bg);
}

lastselected= view;

画像ボタンが表示された後、getview は次の表示項目のために同じビューをリサイクルします。これを修正する方法がわかりません。

4

4 に答える 4

1

使用する

listview.setSelector(drawable)

セレクターを変更するか、無効にする場合。

于 2013-08-12T11:06:57.497 に答える
0

これを試して:

リストビューのスクロール中に選択されたアイテムの問題

遅延読み込みのため、ビューの状態は保証されません。追加のブール値を格納するか、場合によっては HashMap に別の別の文字列のペアを格納して、曲が選択されたかどうかをフラグする方がはるかに正確です。ViewHolders を実装しても害はありません。お役に立てれば。

于 2013-08-12T09:07:02.740 に答える