How to highlight row in ListView in Android?

后端 未结 8 1506
天涯浪人
天涯浪人 2020-11-27 06:05

I need to highlight a row in a ListView that was selected (to show the user what he chose), so, it\'s not the one that is going to be chosen, it\'s the one he c

8条回答
  •  悲&欢浪女
    2020-11-27 06:24

    It's much easier to implement this in your layout files and let Android handle the rest...

    1) Make sure you have android:choiceMode="" set on your ListView layout (singleChoice, multipleChoice, etc). By default it is set to none.

     
    

    2) Create a state selector XML file and save it in your drawables folder. In this example, we'll name it state_selector.xml:

    
    
        
        
        
    
    

    3) In your list item layout, add the state_selector.xml file as the background:

    
      
    
        
    
    
    

    If you are using multipleChoice, you can override onItemClick()and set/unset selected items accordingly. Android will change the background color as specified in your state_selector.xml file.

提交回复
热议问题