zoukankan      html  css  js  c++  java
  • android listview selector

    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 would 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 should 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 forget the android:state_selected. It works like android:state_focused for 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"
        
    />

  • 相关阅读:
    SAP MM 启用批次管理的物料MB21创建预留单据时批次号可以为空!
    强化学习十大原则
    新手必看:生成对抗网络的初学者入门指导
    华为云总裁郑叶来:易获取、用得起、方便用的算力是人工智能发展的关键
    贝叶斯、香农、奥卡姆合写博客「机器学习是什么」
    诗人般的机器学习,ML工作原理大揭秘
    为什么AI的翻译水平还远不能和人类相比?
    2018-8-10-wpf-DoEvents-
    2018-2-13-win10-uwp-iot
    2019-9-11-.NET-Standard
  • 原文地址:https://www.cnblogs.com/tt_mc/p/2526599.html
Copyright © 2011-2022 走看看