2

ListView があり、リストで選択した項目の色を変更したいと考えています。以下の選択したアイテムの色を変更できますが、他のすべてのアイテムの色を元の色に戻す (または変更する) にはどうすればよいですか?

@Override
    public void onListItemClick(ListView parent, View v, int position, long id) 
        v.setBackgroundColor(Color.CYAN);
    }

親の色を変更しようとしましたが、アイテムの色は変わりません:

 @Override
    public void onListItemClick(ListView parent, View v, int position, long id) 
        parent.setBackgroundColor(Color.WHITE);
        v.setBackgroundColor(Color.CYAN);
    }

リストビュー:

        <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:drawSelectorOnTop="false" />

各項目は次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contacts_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout android:id="@+id/linear"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:text=""/>

    <TextView
        android:id="@+id/lastname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="3sp"
        android:text=""/>

    <TextView
        android:id="@+id/firstname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginLeft="3sp"
        android:text=""/>

    <TextView
        android:id="@+id/sipExt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColor="#59696E"
        android:gravity="right"
        android:layout_marginRight="9dp"
        android:textStyle="italic"
        android:text=""/>

</LinearLayout>

    <TextView
    android:id="@+id/alias"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="#a69d86"
    android:textStyle="italic"
    android:layout_below="@id/linear"
    android:text=""/>

4

4 に答える 4

3

次のようにして、ビューを含む親からすべてのアイテムを取得し、すべてのアイテムの背景色を設定することができます

public void onListItemClick(ListView parent, View v, int position, long id){

            //Set background of all items to white
            for (int i=0;i<parent.getChildCount();i++){
                parent.getChildAt(i).setBackgroundColor(Color.WHITE);
            }

            v.setBackgroundColor(Color.CYAN);
}
于 2014-04-08T17:10:21.553 に答える
3

セレクターを使用します。

カスタム レイアウト xml の必要なビューへ

 android:background="@drawable/bkg.xml"

drawable フォルダー内の bkg.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
        android:drawable="@drawable/pressed" />
    <item  android:state_focused="false" 
        android:drawable="@drawable/normal" />
</selector>

要件に応じて以下をカスタマイズします

drawable フォルダー内の pressed.xml

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" >
 <solid android:color="#ff33ffff" />
 <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
 <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
 </shape>

drawable フォルダー内の normal.xml

  <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">  // rectangle shape
  <solid android:color="@android:color/transparent"/>   
  <stroke android:width="3dp"
        android:color="#0FECFF" /> 
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp" // rounded corner
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>  
于 2013-07-24T15:39:13.757 に答える
0

これには CustomAdapter を使用する必要があり、その中で選択した位置インデックスを含むグローバル int 変数を宣言する必要があり、アダプターの getView メソッド内で、選択した位置と getView メソッドの位置値の一致を確認し、それに応じて行ビューの背景を変更します。 .

于 2013-07-24T15:39:56.933 に答える