ExpandableListView详解


前言:在csdn学习也有段时间了,最感谢的就是启舰了,跟他学到很多东西,最喜欢他的那句话:“青春本来就是迷茫的样子,但是不要让未来的你讨厌现在的自己”。最近看书看到ExpandableListView,以前听说过,但是没怎么用过,想着既然不熟悉就练练吧,略有小成就想到为什么不分享一下呢?虽然不是什么难点,但是万一能帮到别人呢?也是一种快乐。

效果图:


一:主页面布局:

<RelativeLayout 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:background="@android:color/white"
    >

    <ExpandableListView
        android:id="@+id/ex_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:divider="@android:color/transparent"
        >
    </ExpandableListView>

</RelativeLayout>

因为我要做出文章目录的感觉,所以用  android:divider="@android:color/transparent"来去掉分割线,看起来有点笨,你有更好的方法评论告诉我哦。

二:主页面代码

ex_list = (ExpandableListView) findViewById(R.id.ex_list);
		String[] group = { "第一章", "第二章", "第三章", "第四章", "第五章", "第六章" };
		String[][] child = { { "张天师祈禳瘟疫,洪太尉误走妖魔", "王教头私走延安府,九纹龙大闹史家村", "史大郎夜走华阴县,鲁提辖拳打镇关西" },
				{ "赵员外重修文殊院,鲁智深大闹五台山", "小霸王醉入销金帐,花和尚大闹桃花村", "九纹龙剪径赤松林,鲁智深火烧瓦罐寺" },
				{ "花和尚倒拔垂杨柳,豹子头误入白虎堂", "林教头刺配沧州道,鲁智深大闹野猪林", "柴进门招天下客,林冲棒打洪教头" },
				{ "花和尚单打二龙山,青面兽双夺宝珠寺", "美髯公智稳插翅虎,宋公明私放晁天王" },
				{ "林冲水寨大并火,晁盖梁山小夺泊", "梁山泊义士尊晁盖,郓城县月夜走刘唐", "横海郡柴进留宾,景阳冈武松打虎" },
				{ "鲁智深浙江坐化,宋公明衣锦还乡", "宋公明神聚蓼儿洼,徽宗帝梦游梁山泊" }
		};
找到ExpandableListView对象ex_list,并准备两个数组,group是用来显示外层的数据,child用来显示内层数据。

adapter = new ExpandAdapter(group, child, MainActivity.this);
		ex_list.setAdapter(adapter);
		//去掉系统的指示器
		ex_list.setGroupIndicator(null);
		//默认展开
		for (int i = 0; i < adapter.getGroupCount(); i++) {
			ex_list.expandGroup(i);
		}
绑定adapter,传输数据源

//外层列表点击事件
		ex_list.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
				Toast.makeText(MainActivity.this, "[" + adapter.getGroup(groupPosition) + "]", Toast.LENGTH_SHORT)
						.show();
				return true;
			}
		});
		//内层列表点击事件
		ex_list.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition,
					long id) {
				Toast.makeText(MainActivity.this, "[" + adapter.getChild(groupPosition, childPosition) + "]",
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});
	}

两个点击事件的处理,值得一提的是group的return true可以屏蔽点击以后关闭或者开启child的功能,因为文章目录嘛,我返回true把它屏蔽掉。

三:ExpandAdapter

private String[]group;
	private String[][]child;
	private Context context;
	
	public ExpandAdapter(String[] group, String[][] child, Context context) {
		super();
		this.group = group;
		this.child = child;
		this.context = context;
	}
构造方法,给参数赋值

ExpandAdapter继承BaseExpandableListAdapter

会重写下面方法

@Override
	public int getGroupCount() {//外层列表的个数
		return group.length;
	}

	@Override
	public int getChildrenCount(int groupPosition) {//内层列表的个数
		return child[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {//外层列表的对象
		return group[groupPosition];
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {//内层列表的对象
		return child[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {//外层列表指针
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {//内层列表指针
		return childPosition;
	}

可见内外是完全分开的,并不是嵌套的关系

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		 convertView = LayoutInflater.from(context).inflate(R.layout.text, null);
		 TextView tv = (TextView) convertView.findViewById(R.id.tv);
		 tv.setText(group[groupPosition]);
		 tv.setTextColor(context.getResources().getColor(R.color.red));
		 tv.setTextSize(16);
		return convertView;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		 convertView = LayoutInflater.from(context).inflate(R.layout.text, null);
		 TextView tv = (TextView) convertView.findViewById(R.id.tv);
		 tv.setText(child[groupPosition][childPosition]);
		 tv.setTextColor(context.getResources().getColor(R.color.blue));
		 tv.setTextSize(14);
		return convertView;
	}

熟悉listview的你肯定对这个不陌生,主要就是加载布局,给控件赋值

四:text布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="111"
        android:padding="10dp"
        />

</LinearLayout>

好了,那这篇文章就结束了,晚安。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值