Android实战简易教程-第十一枪(树形组件:ExpandableListView显示和动态添加删除)

本文介绍如何在Android中使用ExpandableListView组件进行数据分组管理。通过创建自定义适配器和布局文件,实现了显示及动态添加删除功能。详细讲解了包括设置子项点击、组项点击、分组展开和关闭的监听事件。
摘要由CSDN通过智能技术生成

ListView组件可以为用户提供列表的显示功能,但是如果想对这些列表数据进行分组管理,则需要使用android.widget.ExpandableListView组件完成。

与ListView组件一样,如果想要进行数据显示的设置,也需要一个适配器类,但是此时不再继承之前的BaseAdapter,而是继承BaseExpandableListAdapter类完成,此类为抽象类,所以要实现其中的所有抽象方法。

一、创建ExpandableListView

1.定义适配器类-MyExpandableListAdapter.java

package org.yayun.demo;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	public String[] groupStrings = { "我的好友", "我的家人", "同事", "狐朋狗友" };
	public String[][] childStrings = { { "瓜瓜", "太湖", "老皮", "磨叽" },
			{ "大姐", "肥肥", "二姐", "爸爸" }, { "张工", "程序猿" }, { "大鹏", "二妞" } };
	private Context context;

	MyExpandableListAdapter(Context context) {
		this.context = context;
	}

	public int getGroupCount() {// 自动覆写
		return this.groupStrings.length;
	}

	public int getChildrenCount(int groupPosition) {// 自动覆写
		return childStrings[groupPosition].length;
	}

	public Object getGroup(int groupPosition) {// 自动覆写
		return groupStrings[groupPosition];
	}

	public Object getChild(int groupPosition, int childPosition) {// 自动覆写
		return childStrings[groupPosition][childPosition];
	}

	public long getGroupId(int groupPosition) {// 自动覆写
		return groupPosition;
	}

	public long getChildId(int groupPosition, int childPosition) {// 自动覆写
		return childPosition;
	}

	public boolean hasStableIds() {// 自动覆写
		return true;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {// 自动覆写
		TextView textView = buildTextView();
		textView.setText(this.getGroup(groupPosition).toString());
		return textView;
	}

	private TextView buildTextView() {
		AbsListView.LayoutParams params = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 35);// 指定布局参数
		TextView textView = new TextView(this.context);// 创建TextView
		textView.setLayoutParams(params);// 设置布局参数
		textView.setTextSize(15.0f);
		textView.setGravity(Gravity.LEFT);// 左对齐
		textView.setPadding(40, 8, 3, 3);// 间距
		return textView;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {// 自动覆写
		TextView textView = buildTextView();
		textView.setText(getChild(groupPosition, childPosition).toString());
		return textView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {// 自动覆写
		return true;
	}

}


2.定义布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/elistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


3.定义MainActivity.java:

package org.yayun.demo;

import android.app.Activity;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

public class MainActivity extends Activity {
	private ExpandableListView expandableListView;
	private ExpandableListAdapter adapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
	expandableListView=(ExpandableListView)findViewById(R.id.elistview);
	adapter=new MyExpandableListAdapter(this);
	expandableListView
  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值