Refreshing checkbox state in a listview

后端 未结 2 1766
轻奢々
轻奢々 2020-12-18 15:25

I have list view with custom row layout (list_row.xml) that contains CheckBox element:



        
相关标签:
2条回答
  • 2020-12-18 15:36

    What I have found is that because getView is redrawing and recycling the list item on scroll one needs to state explicitly when the check box should be checked and when not (similar solution for the selected item's background colour) in a statment like

    if (condition){...set check box checked code...}else{...set check box unchecked code...}

    The checkbox state can be derived from an array which must be updated every time the user check or uncheck the box in the list view.

    0 讨论(0)
  • 2020-12-18 15:43

    I'm not sure I followed everything you said there, but keep in mind that ListView recycles views as you scroll. This means that you cannot assume that a view will be in any default state at the start of getView. This is probably why it doesn't work when you stop resetting the checkbox to the correct state every time you get a new view.

    That said, did you consider using ListViews built in checkbox tool? Set its choice mode to CHOICE_MODE_MULTIPLE and use one of their provided layouts, like android.R.layout.simple_list_item_multiple_choice.

    0 讨论(0)
提交回复
热议问题