Create expandable listview with group and child checkbox

前端 未结 2 1016
情话喂你
情话喂你 2021-01-01 02:27

I want to create an expandable listview with checkboxes present in the groups as well as the children. What I want is, the user may check the group in order to select all th

相关标签:
2条回答
  • 2021-01-01 02:40

    Try this sample Code Expandable CheckList

    0 讨论(0)
  • 2021-01-01 02:45

    To do this you will need to include boolean flag in each object, or in you adapter create two list views (for groups and items) that will contain selected items.

    I would rather add boolean like this :

    class Group{ ...
    
      boolean isSelected;
    }
    
    class Item { ...
    
      boolean isSelected;
    }
    

    Then in Adapter in getViews() methods:

    getItemView(...){
      ...
      itemHolder.checkbox.setSelected(item.isSelected)
    }
    
    getGroupView(...){
      ...
      groupHolder.checkbox.setSelected(group.isSelected)
    
      //add lisener
      itemHolder.checkbox.setOnCheckedChangeListener(lisener);
    }
    

    And on group check you can do something like this :

    void selectGroup(Group group){
      for(Item item: group.getItems()){
          item.isSelected = true;
      }
    
      //update your list
      notifiDataSetChanged();
    }
    

    That's all what you need

    UPDATE :

    • Android Developers - UI
    • Android Holder Pattern
    0 讨论(0)
提交回复
热议问题