文章目录
- 效果图
- 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;//先创建,防止NullPointExcaptionprivate ArrayList<Group> groups = new ArrayList<>();String[] groupStrs = {"广东省","湖南省","湖北省","江苏省","云南省","海南省"};String[] guangdong = {"深圳市","广州市","佛山市","东莞市","韶关市"};String[] hunan = {"长沙市","株洲市","衡阳市","湘潭市","益阳市"};String[] hubei = {"武汉市","襄阳市","荆州市","黄冈市","仙桃市"};String[] jiangsu = {"南京市","苏州市","泰州市","无锡市","徐州市"};String[] yunan = {"昆明市","普洱市","曲靖市","玉溪市","丽江市"};String[] hainan = {"海口市","东方市","琼海市","三亚市","文昌市"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initview();initdata();}/** 初始化ExpandableListView等视图*/private void initview(){listview = (ExpandableListView) findViewById(R.id.listview);listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {if (adapter != null){Group group = adapter.getGroup(i);Toast.makeText(MainActivity.this,"点击了"+group.getGroupName(),Toast.LENGTH_SHORT).show();}return false;}});listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {if (adapter != null){Child child = adapter.getChild(groupPosition, childPosition);Toast.makeText(MainActivity.this,"点击了"+child.getChildName(),Toast.LENGTH_SHORT).show();}return false;}});}/** 初始化数据-网络请求数据*/private void initdata(){//若是http请求,最好等数据回调成功后再装配数据(回调函数是主线程可直接装配,分线程可通过Handler再装配)//demo中数据均是伪造for (String groupName : groupStrs){ArrayList<Child> childs = new ArrayList<>();Group group = null;if (groupName.equals("广东省")){for (String name : guangdong){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("湖南省")){for (String name : hunan){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("湖北省")){for (String name : hubei){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("江苏省")){for (String name : jiangsu){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("云南省")){for (String name : yunan){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("海南省")){for (String name : hainan){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}groups.add(group);}if (groups.size() > 0){if (adapter == null){adapter = new AMapAdapter(this,groups);listview.setAdapter(adapter);}}}
}
3.继承BaseExpandableListAdapter(推荐使用)或实现ExpandableListAdapter
public class AMapAdapter extends BaseExpandableListAdapter {private Context mContext;private ArrayList<Group> mGroups;public AMapAdapter(){}public AMapAdapter(Context context, ArrayList<Group> groups){mContext = context;mGroups = groups;}@Overridepublic int getGroupCount() {return mGroups.size();}@Overridepublic int getChildrenCount(int i) {return mGroups.get(i).getChildList().size();}@Overridepublic Group getGroup(int i) {return mGroups.get(i);}@Overridepublic Child getChild(int groupPosition, int childPosition) {return mGroups.get(groupPosition).getChildList().get(childPosition);}@Overridepublic long getGroupId(int i) {return 0;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return 0;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int i, boolean isExpand, View contentView, ViewGroup viewGroup) {GroupViewHolder groupViewHolder = null;if (contentView == null){contentView = View.inflate(mContext, R.layout.group_item,null);groupViewHolder = new GroupViewHolder();groupViewHolder.gName = (TextView) contentView.findViewById(R.id.group_name);contentView.setTag(groupViewHolder);}else{groupViewHolder = (GroupViewHolder) contentView.getTag();}Group group = getGroup(i);groupViewHolder.gName.setText(group.getGroupName());return contentView;}@Overridepublic View getChildView(int groupId, int childId, boolean isLastChild, View contentView, ViewGroup viewGroup) {ChildViewHolder childViewHolder = null;if (contentView == null){contentView = View.inflate(mContext,R.layout.child_item,null);childViewHolder = new ChildViewHolder();childViewHolder.childName = (TextView) contentView.findViewById(R.id.child_name);contentView.setTag(childViewHolder);}else{childViewHolder = (ChildViewHolder) contentView.getTag();}Child child = getChild(groupId, childId);childViewHolder.childName.setText(child.getChildName());return contentView;}@Overridepublic boolean isChildSelectable(int i, int i1) {//若为false,会导致setOnChildClickListener失效return true;}class GroupViewHolder{public TextView gName;}class ChildViewHolder{public TextView childName;}
}
以上的方法跟ListView相似,这里就不累赘了
demo下载:
http://download.csdn.net/detail/lzp2015/9793392
3.去掉ExpandableListView的箭头
在ExpandableListView标签中添加如下属性,即可
android:groupIndicator="@null"
listSelector属性是设置点击Item的颜色
android:listSelector="@drawable/custom_listview_seletor"
如不需要显示右侧滚动条,设置scrollbars属性为none
android:scrollbars="none"
如需自定义grouIndicator
//1.去掉自带的groupIndicator
android:groupIndicator="@null"
//2.在group_item.xml中添加ImagView(根据自己需求去规划)
//3.在getGroupView()方法中添加图片
@Overridepublic View getGroupView(int i, boolean isExpand, View contentView, ViewGroup viewGroup) {if (isExpand){groupViewHolder.expandTag.setBackgroundResource(R.mipmap.list_view_indicate_down);}else{groupViewHolder.expandTag.setBackgroundResource(R.mipmap.list_view_indicate_right);}return contentView;}
4.解决setOnChildClickListener失效问题
将Adapter中isChildSelectable返回值改为true,若还没解决,可能是你childitem中某个视图一直占用了焦点而没释放
//是否允许childItem被选中
@Overridepublic boolean isChildSelectable(int i, int i1) {//若为false,会导致setOnChildClickListener失效return true;}
5.解决collapseGroup(i)崩溃问题
应用场景:
当点击某一个Group时,关闭之前打开的Group
错误代码
结果是出现ANR,并抛出java.lang.IndexOutOfBoundsException
这是ExpandabeListView的bug,解决办法:
listview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {@Overridepublic void onGroupExpand(int groupPosition) {if (oldPostion == -1){oldPostion = groupPosition;}else{listview.collapseGroup(oldPostion);oldPostion = groupPosition;}}
});
6.解决group_item.xml中包含CheckBox、EditText等,点击不能展开的问题
出现此类现象,原因是因为焦点被CheckBox、EditText获取导致的。
解决办法:
在item_item.xml(根据自己的xml文件)找到对应的视图(CheckBox、EditText等),设置其焦点初始值为false,完美解决
android:focusable="false"