Android listview Selector

Source: Internet
Author: User

The solution by dglmtn doesn't work when you have a 9-patch drawable with padding as background. strange things happen, I don't even want to talk about it, if you have such a problem, you know them.

Now, if you want to have a listview with different States and 9-patch drawables (it wocould work with any drawables and colors, I think) You have to do 2 things:

    1. Set the selector for the items in the list.
    2. Get rid of the default selector for the list.

What you shoshould do is first set the row_selector.xml:

<? XML version = "1.0" encoding = "UTF-8" ?>
< Selector Xmlns: Android = "Http://schemas.android.com/apk/res/android"   >
< Item Android: state_enabled = "True"  
Android: state_pressed = "True" Android: drawable = "@ Drawable/list_item_bg_pressed"   />
< Item Android: state_enabled = "True"
Android: state_focused = "True" Android: drawable = "@ Drawable/list_item_bg_focused"   />
< Item Android: state_enabled = "True"
Android: state_selected = "True" Android: drawable = "@ Drawable/list_item_bg_focused"   />
< Item
Android: drawable = "@ Drawable/list_item_bg_normal"   />
</ Selector >

 

Don't forgetAndroid: state_selected. It works likeAndroid: state_focusedFor the list, but it's applied for the list item.

Now apply the selector to the items (row. XML ):

<? XML version = "1.0" encoding = "UTF-8" ?>
< Relativelayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: layout_width = "Fill_parent"
Android: layout_height = "Wrap_content"
Android: Orientation = "Horizontal"
Android: Background = "@ Drawable/row_selector"
>
...
</ Relativelayout >

 

Make a selector for the list (to get rid of the default one which ruins the effect)-list_selector.xml:

 

<? XML version = "1.0" encoding = "UTF-8" ?>
< Selector Xmlns: Android = "Http://schemas.android.com/apk/res/android"   >
< Item Android: state_enabled = "True"  
Android: state_pressed = "True" Android: drawable = "@ Color/Android: Transparent"   />
< Item Android: state_enabled = "True"
Android: state_focused = "True" Android: drawable = "@ Color/Android: Transparent"   />
< Item
Android: drawable = "@ Color/Android: Transparent"   />
</ Selector >

Notice that it makes all the States transparent. That's what I meant by getting rid of the default states.

Last, but not least-apply the selector to the listview:

 <Listview
Android: ID= "@ + ID/Android: List"
...
Android: listselector= "@ Drawable/list_selector"
/>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.