文章目录
- 效果图
- ExpandableListView的简介与使用
- 去掉ExpandableListView的箭头以及自定义Indicator
- 解决setOnChildClickListener失效问题
- 解决collapseGroup(i)崩溃问题
- 解决group_item.xml中包含CheckBox、EditText等,点击不能展开的问题
1.效果图
2.ExpandableListView的简介与使用
ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew 显示的列表项应该由ExpandableAdapter提供。
- 重要方法
//在分组列表中展开指定组(若想全部展开,循环分组列表再调用此方法)
expandGroup (int groupPos) ;
//在分组列表中关闭指定组(若想全部展开,循环分组列表再调用此方法)
collapseGroup(int groupPos);
//设置选择指定的组
setSelectedGroup (int groupPosition) ;
//设置选择指定的子项
setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);
//返回所选择的组
getPackedPositionGroup (long packedPosition);
//返回所选择的子项
getPackedPositionForChild (int groupPosition, int childPosition) ;
//返回所选择项的类型(Child/Group)
getPackedPositionType (long packedPosition);
//判断此组是否展开
isGroupExpanded (int groupPosition);
- 使用步骤
1.在布局文件中引用ExpandableListView
<ExpandableListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:childDivider="#cccccc"
android:divider="#cccccc"
android:dividerHeight="1px"
/>
2.初始化与装配数据
public class MainActivity extends AppCompatActivity {
private ExpandableListView listview;
private AMapAdapter adapter;
//先创建,防止NullPointExcaption
private ArrayList<Group> groups = new ArrayList<>();
String[] groupStrs = {
"广东省","湖南省","湖北省","江苏省","云南省","海南省"};
String[] guangdong = {
"深圳市","广州市","佛山市","东莞市","韶关市"};
String[] hunan = {
"长沙市","株洲市","衡阳市",