ListView Item selection Android

后端 未结 6 1054
夕颜
夕颜 2020-12-11 19:04

I have a listview in that i want when my activity starts then the first item will have background and the other items don\'t have any background.After that if the user selec

6条回答
  •  佛祖请我去吃肉
    2020-12-11 19:42

    For doing this what you can do is create a selector and apply it to parent view of your row.xml

    • Define color in strings.xml

      #009FE3

    • Create a selector

      
      
      

    Now apply it as android:background="@drawable/row_selector" for parent view of your row.

    • Set ListView as CHOICE_MODE_SINGLE in java code

      listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    • And finally setAdapter to your ListView.

      listview.setAdapter(adapter);

    • For selection of default item in ListView use,

      listview.setItemChecked(position, true);

    I had created a demo example for the same you can download it from my github

提交回复
热议问题