简体   繁体   中英

Change Background Color Of Selected Item In ListView

I have a ListView, I would like that each time i select an item in the listview, the background color changes to grey. If I select another element I want previous elements background to revert to white and the new item's background to be highlighted in grey

If you just want list items to change background color based on selection, that sounds like basic selector functionality. Create a selector xml file and set it as the background in your row xml.

Something like:

 < selector xmlns:android="http://schemas.android.com/apk/res/android">
    < item android:drawable="@android:drawable/myGreyColor" android:state_pressed="true"/>
    < item android:drawable="@android:drawable/myGreyColor" android:state_selected="true"/>
    < item android:drawable="@android:drawable/myWhiteColor"/>
    < /selector>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM