2

現在、各行に Radiobutton と TextView を含む ListView があります。ListView はカスタム アダプターを使用し、ListView に android:choiceMode="singleChoice" ディレクティブを指定して、リストから 1 つの要素のみを選択できるようにします。ただし、選択したアイテムを強調表示できないようです。

ListView の XML 宣言は次のとおりです。

<ListView android:id="@+id/employees_list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="2dp"
                android:paddingRight="1dp"
                android:background="@drawable/borderlist"
                android:choiceMode="singleChoice" 
                android:listSelector="#48ad82"
                android:layout_below="@id/employees_header">
 </ListView>

ListView の行の XML 宣言は次のとおりです。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row_employee"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingTop="13dp"
    android:paddingBottom="13dp"
    android:descendantFocusability="blocksDescendants">

    <RadioButton android:id="@+id/radio_employee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"/>

    <TextView
        android:id="@+id/text_employeename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_toRightOf="@id/radio_employee"
        android:layout_marginTop="3dp"
        android:layout_marginLeft="5dp"
        android:focusable="false" />

</RelativeLayout> 

行の色を交互に変更したいので、プログラミングによって各行にセレクターを設定します。カスタム アダプターの getView に次のコードを記述します。

// Alternate row colors.
    if (position % 2 == 0) {
            v.setBackgroundResource(R.drawable.selector_list1);
        } else {
            v.setBackgroundResource(R.drawable.selector_list2);
    }

ここでは、selector_list1 の XML 宣言を示します。selector_list2 は、最初のドローアブルを除いて同じです。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:state_pressed="false"  android:drawable="@color/white" />
    <item android:state_pressed="true" android:drawable="@color/lightgreen_listpressed" />
    <item android:state_selected="true" android:state_pressed="false"  android:drawable="@color/lightgreen_listpressed" />
</selector>

State_pressed は機能しますが、state_activated、state_selected、state_enabled、state_checked を使用してみましたが、選択したリスト項目の色を変更するために何もうまくいきません。私は何か間違ったことをしていますか?

編集 1: 単純にセレクターをウィンドウから放り出す (IE は使用しない) 場合、選択の色付けは、ListView 宣言にある android:listSelector="#48ad82" ディレクティブでのみ機能します。これがセレクターで機能しないのはなぜですか?

編集 2: listSelector ディレクティブを完全に削除するときに state_focused を含む、すべての異なる状態をもう一度試しました。まだ結果が出ていません。

EDIT 3: 問題は、ListView 自体が選択を許可し、RelativeLayout ビューが実際に「選択」されることはないため、セレクターを設定しても意味がないことだと思います。その場合、どこでどのようにセレクターを設定して、私がやりたいことを実行させる必要がありますか?

EDIT 4: このチュートリアルに基づいてアダプターに色を設定してみました: http://eureka.ykyuen.info/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in- ListView 要素のすべての要素を非表示にする android: drawSelectorOnTop ="true" を呼び出さない限り、結果はありません。

編集 5: 今のところこれを削除し、交互の色を完全に削除します。私が StackOverflow や Google で見つけた説明のどれも、この問題について話していません。これを「機能」させる唯一の方法は、android:drawSelectorOnTop="true" を呼び出すことです。これにより、ListView で現在選択されている要素の色が変更されますが、その 1 行のすべてが非表示になります。

4

0 に答える 0