3

I have my custom listselector like this :

<?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/bg_list_hover"></item>
    <item 
        android:state_focused="true"
        android:drawable="@drawable/bg_list"></item>
</selector>

and my listview :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_dasar" >
    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="StoreCategory" 
                android:textSize="15sp"
                android:paddingLeft="10dip"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:textColor="@color/textcolor"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:listSelector="@drawable/listselector">
    </ListView>

my problem is, when i do not click on listview, listview item do not use focused state background (@drawable/bg_list) ...

any one can help me please???

4

4 に答える 4

5

それでも必要な場合:重要なのは、listSelectorをリストアイテムの背景として設定することです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#819FF7"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/mainList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#819FF7"
        />

 </LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lineItem"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector">

     <ImageView android:id="@+id/imageView"
        android:layout_width="96dp"
        android:layout_height="64dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_toRightOf="@+id/imageView"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip" 
        android:layout_centerVertical="true"
        android:textSize="18sp"
        android:textColor="#ff000000" 
        android:layout_width="wrap_content"
        />
    <Button 
        android:id="@+id/button" 
        android:gravity="center"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp" 
        android:layout_alignParentRight="true"
        android:focusable="false" 
        android:text="Delete" />

</RelativeLayout>
于 2012-10-31T16:37:07.227 に答える
3

私はいくつかの参照を読んで、私の問題を解決しました..私のリストセレクターは次のようになります:

<?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/bg_list_hover" />
 <item 
     android:drawable="@drawable/bg_list" />
</selector>

私のリストビューのレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg_dasar" >
    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="StoreCategory" 
                android:textSize="15sp"
                android:paddingLeft="10dip"
                android:paddingTop="10dip"
                android:paddingRight="10dip"
                android:textColor="@color/textcolor"/>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        **android:listSelector="@android:color/transparent"**>
    </ListView>

</LinearLayout>

また、リストセレクターをリストアイテムの背景に追加します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="horizontal" 
    android:padding="15dip"
    android:gravity="center_vertical"
    **android:background="@drawable/listselector"**>

    <ImageView
        android:id="@+id/imageViewlogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dip" />

    <TextView
        android:id="@+id/textViewcategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@color/textcolor" 
        android:textSize="15sp"/>

</LinearLayout>

私の質問に答えてくれたジェイとダンウィルキーに感謝.. :))

于 2012-10-25T15:40:04.853 に答える
0

xml で android:focusable="true" を設定した後、コードの listView で "requestFocus()" を呼び出してみましたか? ビュー ドキュメントから:

「これを呼び出して、特定のビューまたはその子孫の 1 つにフォーカスを与えようとします。ビューがフォーカス可能でない場合 (isFocusable() が false を返す)、またはフォーカス可能であるがフォーカス可能でない場合、ビューは実際にはフォーカスを取得しません。デバイスがタッチ モードにあるときのタッチ モード (isFocusableInTouchMode()). focusSearch(int) も参照してください。引数 FOCUS_DOWN と null を指定して requestFocus(int, Rect) を呼び出します。」

于 2012-10-25T14:02:16.863 に答える
0

現時点では、セレクター xml は、アイテムをクリックしたときに bg_list_hover を使用し、アイテムにフォーカスしたときに (たとえば、トラックボールを使用して) bg_list を使用するように言っているようです。

アイテムがクリックもフォーカスもされていないデフォルトのケースでカスタム ドローアブルを使用する場合は、そのためのエントリを追加する必要があります。

私は次のようなものを使用します:

<?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/bg_list_pressed"></item>
    <item 
        android:state_focused="true"
        android:drawable="@drawable/bg_list_focused"></item>
    <item 
        android:drawable="@drawable/bg_list"></item>
</selector>
于 2012-10-25T14:04:23.550 に答える