
新建主界面 xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity"> <ExpandableListVIEw androID:ID="@+ID/expandableListvIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> </ExpandableListVIEw></linearLayout>
新建一级目录xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#FFFFFF"> <TextVIEw androID:layout_wIDth="48dp" androID:layout_height="16dp" androID:text="第一关" androID:textcolor="#ff777777" androID:textSize="16dp" /></linearLayout>
新建二级目录xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:gravity="center_vertical"> <ImageVIEw androID:layout_wIDth="29.2dp" androID:layout_height="25.4dp" androID:src="@mipmap/z_dialog1_dianshi"/> <TextVIEw androID:layout_marginleft="10dp" androID:ID="@+ID/text1" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="内容" androID:textSize="14dp" androID:gravity="center_vertical" androID:textcolor="#ff777777" /></linearLayout>
编写适配器
package com.rhkj.zhihuixue.activity.zhijizhiyi.adapter;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.Baseexpandablelistadapter;import androID.Widget.TextVIEw;import com.rhkj.zhihuixue.R;import java.util.List;/** * @Classname HotelEntityAdapter * @Description Todo * @Author ZhangXueTao * @Date 2020/10/27 14:44 * @Version 1.0 模板 */public class HotelEntityAdapter extends Baseexpandablelistadapter { List<String> mGroupList;//一级List List<List<String>> mChildList;//二级List 注意!这里是List里面套了一个List<String>,实际项目你可以写一个pojo类来管理2层数据 public HotelEntityAdapter(List<String> groupList, List<List<String>> childList){ mGroupList = groupList; mChildList = childList; } @OverrIDe public int getGroupCount() {//返回第一级List长度 return mGroupList.size(); } @OverrIDe public int getChildrenCount(int groupposition) {//返回指定groupposition的第二级List长度 return mChildList.get(groupposition).size(); } @OverrIDe public Object getGroup(int groupposition) {//返回一级List里的内容 return mGroupList.get(groupposition); } @OverrIDe public Object getChild(int groupposition, int childposition) {//返回二级List的内容 return mChildList.get(groupposition).get(childposition); } @OverrIDe public long getGroupID(int groupposition) {//返回一级VIEw的ID 保证ID唯一 return groupposition; } @OverrIDe public long getChildID(int groupposition, int childposition) {//返回二级VIEw的ID 保证ID唯一 return groupposition + childposition; } /** * 指示在对基础数据进行更改时子ID和组ID是否稳定 * @return */ @OverrIDe public boolean hasStableIDs() { return true; } /** * 返回一级父VIEw */ @OverrIDe public VIEw getGroupVIEw(int groupposition, boolean isExpanded, VIEw convertVIEw, VIEwGroup parent) { convertVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.z_exList_one_item, parent,false);// ((TextVIEw)convertVIEw).setText((String)getGroup(groupposition)); return convertVIEw; } /** * 返回二级子VIEw */ @OverrIDe public VIEw getChildVIEw(int groupposition, int childposition, boolean isLastChild, VIEw convertVIEw, VIEwGroup parent) { convertVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_item, parent,false);// ((TextVIEw)convertVIEw).setText((String)getChild(groupposition,childposition)); return convertVIEw; } /** * 指定位置的子项是否可选 */ @OverrIDe public boolean isChildSelectable(int groupposition, int childposition) { return true; }}Activitypackage com.rhkj.zhihuixue.activity.zhijizhiyi;import androIDx.appcompat.app.AppCompatActivity;import androIDx.recyclervIEw.Widget.linearlayoutmanager;import androIDx.recyclervIEw.Widget.RecyclerVIEw;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.ExpandableListVIEw;import androID.Widget.Toast;import com.rhkj.zhihuixue.R;import com.rhkj.zhihuixue.activity.zhijizhiyi.adapter.HotelEntityAdapter;import com.rhkj.zhihuixue.base.app.ZBaseLandscapeActivity;import java.util.ArrayList;import java.util.List;import butterknife.BindVIEw;public class MainActivity2 extends ZBaseLandscapeActivity { @BindVIEw(R.ID.expandableListvIEw) ExpandableListVIEw expandableListvIEw; private HotelEntityAdapter mAdapter; @OverrIDe public int getLayoutID() { return R.layout.activity_main2; } @OverrIDe public voID initPresenter() { } @OverrIDe public voID initVIEw() { List<String> groupList = new ArrayList<>(); groupList.add("一"); groupList.add("二"); groupList.add("三"); List<List<String>> childList = new ArrayList<>(); List<String> childList1 = new ArrayList<>(); childList1.add("1"); childList1.add("1"); childList1.add("1"); List<String> childList2 = new ArrayList<>(); childList2.add("2"); childList2.add("2"); childList2.add("2"); List<String> childList3 = new ArrayList<>(); childList3.add("3"); childList3.add("3"); childList3.add("3"); childList.add(childList1); childList.add(childList2); childList.add(childList3); mAdapter = new HotelEntityAdapter(groupList, childList); expandableListvIEw.setAdapter(mAdapter); expandableListvIEw.setonGroupClickListener(new ExpandableListVIEw.OnGroupClickListener() {//一级点击监听 @OverrIDe public boolean onGroupClick(ExpandableListVIEw parent, VIEw v, int groupposition, long ID) { //如果你处理了并且消费了点击返回true,这是一个基本的防止ontouch事件向下或者向上传递的返回机制 return false; } }); expandableListvIEw.setonChildClickListener(new ExpandableListVIEw.OnChildClickListener() {//二级点击监听 @OverrIDe public boolean onChildClick(ExpandableListVIEw parent, VIEw v, int groupposition, int childposition, long ID) { //如果你处理了并且消费了点击返回true return false; } }); }}其他Xml属性androID:divIDerHeight="20dp" 设置item间距高度,注意设置这个间距包括了一级和二级
androID:divIDer="@color/colorRed1" 设置一级间距颜色
androID:childdivIDer="@color/colorGreen" 设置二级间距颜色
androID:childindicator:显示在子列表旁边的Drawable对象,可以是一个图像
androID:childindicatorEnd:子列表项指示符的结束约束位置
androID:childindicatorleft:子列表项指示符的左边约束位置
androID:childindicatorRight:子列表项指示符的右边约束位置
androID:childindicatorStart:子列表项指示符的开始约束位置
androID:groupIndicator:显示在组列表旁边的Drawable对象,可以是一个图像
androID:indicatorEnd:组列表项指示器的结束约束位置
androID:indicatorleft:组列表项指示器的左边约束位置
androID:indicatorRight:组列表项指示器的右边约束位置
androID:indicatorStart:组列表项指示器的开始约束位置
可以实现的ExpandableListVIEw3种Adapter1. 扩展BaseExpandableListAdpter实现ExpandableAdapter。
2. 使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter
3. 使用simpleCursorTreeAdapter
ExpandableListVIEw的一些API详解
mExpandableListVIEw.collapseGroup(position); 收起指定位置组的二级列表
mExpandableListVIEw.expandGroup(position); 展开指定位置组的二级列表
mExpandableListVIEw.isGroupExpanded(position); 指定位置的组是否展开
mExpandableListVIEw.setSelectedGroup(position); 将指定位置的组设置为置顶
改变方向图标的位置
int wIDth = getResources().getdisplayMetrics().wIDthPixels;mExpandableListVIEw.setIndicatorBounds(wIDth - UnitConversionUtil.dip2px(this,40) , wIDth - UnitConversionUtil.dip2px(this,15));//设置图标位置
关于点击事件的一些坑
如果你在适配器里去实现了Group的点击事件想用回调方法回调出去(如下代码),这个时候你就会碰到一个Group无法展开和收起的坑,原因很简单因为这里已经把点击事件消费了,点击不在继续向下传递,所以底层实现的展开和收起不执行了
/** * 返回一级父VIEw */ @OverrIDe public VIEw getGroupVIEw(final int groupposition, boolean isExpanded, VIEw convertVIEw, final VIEwGroup parent) { convertVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); convertVIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { } }); return convertVIEw; }那么如何解决呢?
方法一不用setonClickListener(),因为这个会消费事件,我们改用setontouchListener,如下代码:
convertVIEw.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN){ //实现你自己的接口回调 } return false; } }); 这里有一点需要注意ACTION_DOWN 需要返回false,因为底层是消费ACTION_DOWN的来展开和收起的....
方式二将groupposition回调到外面后使用collapseGroup() 或者 expandGroup()方法实现.
总结以上是内存溢出为你收集整理的android 二级列表 二级目录全部内容,希望文章能够帮你解决android 二级列表 二级目录所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)