Android listview item background change

前端 未结 3 581
借酒劲吻你
借酒劲吻你 2021-01-18 07:27

i have a android listview. i want to change listview item background when i click one listview item.

and then previous selected item must go back to default backgrou

3条回答
  •  不知归路
    2021-01-18 08:05

    You should use the built in methods of selecting items in a listview. Manually changing the background is prone to error as you have found.

    Add this attribute to the root view in your listview item xml

    android:background="?android:attr/activatedBackgroundIndicator"
    

    then call setItemChecked(x, true) on your ListView where x is the position of the item you want to be selected.

    Ensure your listview has a ChoiceMode set that allows selection (such as "SingleChoice")

提交回复
热议问题