0

私のAndroidコードは、onClickイベントで最初に表示されるリストビュー項目の背景色を緑に変更できますが、リストビューを下にスクロールしてリストをクリックしようとすると、緑が表示されません. この問題を解決する方法を教えてください。コードスニペットをここに添付しました。

   public void onItemClick( AdapterView<?> parent, View item,   
                                  int position, long id) {



     for(int i=0; i<parent.getChildCount(); i++)
           {
               Log.d("TAG", "Number of times printed");
                if(i == position)
                {
                          parent.getChildAt(position).setBackgroundColor(Color.GREEN);
                         // Log.d("TAG","Green at position :: " + position);

                }
                else
                {
                          parent.getChildAt(i).setBackgroundColor(Color.GRAY);
                }
            }
}  
4

2 に答える 2

1

値フォルダーの color.xml ファイルで色を定義します

ドローアブル フォルダー listViewBackground.xml に次のコードを含む xml ファイルを作成します。

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" 
      android:state_pressed="false" 
      android:drawable="@color/grey" />

<item android:state_focused="true" 
      android:state_pressed="true"
      android:drawable="@color/green"

       />

<item android:state_focused="false" 
      android:state_pressed="true"
      android:drawable="@color/green" />

<item android:drawable="@color/grey" />

このファイルをリスト項目の背景として設定します

于 2012-09-24T07:15:01.120 に答える
0

それ以外の

                  parent.getChildAt(position).setBackgroundColor(Color.GREEN);

これを試して

    parent.getChildAt(position).setBackgroundColor(R.color.orange);

値フォルダーのcolor.xmlで色を定義する必要があります--vales/color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="orange">#e68c10</color>
</resources>

また、関数を探して使用してみてください

public void onListItemClick(ListView parent,View v,int position,long id){}
于 2012-09-24T04:38:45.640 に答える