SimpleExpandableListAdapter



SimpleExpandableListAdapter(context, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo)

主要讲解这个适配器的使用方法

context:一般是context 用this

groupData:组的数据 一般是Arraylist   一级条目数据

groupLayout:组所使用的布局文件

groupFrom:关键字

groupTo: 指定关键字所显示控件的ID

childData:二级条目数据

childLayout:二级条目的布局

childFrom:二级条目的关键字

childTo:二级条目所指定的控件ID


这个一般与ExpandableListView一起使用





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的展开和收起示例,使用 ExpandableListView 组件实现: 1. 在您的布局文件中添加一个 ExpandableListView 组件: ```xml <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 2. 创建一个适配器类来管理数据和视图。在这个示例中,我们将使用 SimpleExpandableListAdapter 类,它是 BaseExpandableListAdapter 的一个简单实现。创建适配器的代码如下: ```java String[] groups = {"Group 1", "Group 2", "Group 3"}; String[][] children = {{"Child 1", "Child 2", "Child 3"}, {"Child 4", "Child 5", "Child 6"}, {"Child 7", "Child 8", "Child 9"}}; List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); for (int i = 0; i < groups.length; i++) { Map<String, String> curGroupMap = new HashMap<String, String>(); groupData.add(curGroupMap); curGroupMap.put("GROUP_NAME", groups[i]); List<Map<String, String>> childrenList = new ArrayList<Map<String, String>>(); for (int j = 0; j < children[i].length; j++) { Map<String, String> curChildMap = new HashMap<String, String>(); childrenList.add(curChildMap); curChildMap.put("CHILD_NAME", children[i][j]); } childData.add(childrenList); } SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, android.R.layout.simple_expandable_list_item_1, new String[] {"GROUP_NAME"}, new int[] {android.R.id.text1}, childData, android.R.layout.simple_expandable_list_item_2, new String[] {"CHILD_NAME"}, new int[] {android.R.id.text1} ); ``` 3. 将适配器设置为 ExpandableListView 的适配器: ```java ExpandableListView expandableListView = findViewById(R.id.expandableListView); expandableListView.setAdapter(adapter); ``` 4. 设置 ExpandableListView 的 OnGroupClickListener 和 OnChildClickListener,以处理用户单击组或子项时的操作: ```java expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { if (parent.isGroupExpanded(groupPosition)) { parent.collapseGroup(groupPosition); } else { parent.expandGroup(groupPosition); } return true; } }); expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // 处理子项点击事件 return true; } }); ``` 在这个示例中,我们使用 SimpleExpandableListAdapter 类来创建适配器,并在 OnGroupClickListener 中实现展开和收起组的逻辑。当用户单击组时,我们检查它是否已经展开,如果是,则将其收起;如果没有,则将其展开。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值