Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

android - ListView selected item drawable

I have a problem in my ListView. I want to change the background of selected item when I select it to a custom drawable that I have, but it doesn't work.

I want to have this effect as in the image to be the selecteditem

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" 
          android:drawable="@drawable/selected_item"/>
    <item android:drawable="@android:color/transparent" />
</selector>

And this is the text of list_row

<!-- this layout is used to view row of category list and resturant list  -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/txt_category_row" 
    android:gravity="center"
    android:layout_width="match_parent" 
    android:textColor="@color/BLACK"
    android:layout_height="55dp" 
    android:padding="10dp"
    android:text="@string/hello" 
    android:layout_marginTop="5dp"

    android:textAppearance="?android:attr/textAppearanceLarge" />

And this the ListView code.xml:

<ListView android:id="@+id/list_category" 
    android:layout_alignParentLeft="true" 
    android:layout_height="fill_parent" 
    android:layout_width="184dp" 
    android:dividerHeight="2px"
    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    android:layout_marginTop="5dp"  
    android:listSelector="@drawable/list_selector"
    android:divider="@color/Gray"  >
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In touch mode there is no selected or focused state.

However, you can have a checked state (even without a checkbox) and use that to change properties upon "selection". In your java code where you set up to display your list add this line after you define the listview:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Then in your state list XML change this:

android:state_selected="true"

to this:

android:state_activated="true"

So now you should have:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_activated="true"  
        android:drawable="@drawable/selected_item"/> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

And finally, set the background for your row view to point to your selector file:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/txt_category_row"  
    android:gravity="center" 
    android:layout_width="match_parent"  
    android:textColor="@color/BLACK" 
    android:layout_height="55dp"  
    android:padding="10dp" 
    android:text="@string/hello"  
    android:layout_marginTop="5dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:background="@drawable/item_selector" /> 

You didn't give a name for your state list XML so I just used "item_selector". You should replace that with whatever the name of that file actually is.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...