android 三级菜单 BaseExpandableListAdapter

在网上搜了很长时间,没有找到合适的Android三级菜单,所以就自己动手写了一个,主要使用了BaseExpandableList来实现,通过三个布局文件来完成对应的菜单项,具体实现请参照下图。

通过上面两图可以看出为三级菜单,每一个菜单对应一个实体类,可以通过json解析数据加载进来,这里就不过多解释了,直接上源码!

Activity实现类:

package com.zkq.activity;

import java.util.ArrayList;
import java.util.List;

import com.example.teltest.R;
import com.zkq.model.FirstItem;
import com.zkq.model.SecondItem;
import com.zkq.model.ThirdItem;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

import com.zkq.adapter.*;

public class MainActivity extends Activity {
	private ExpandableListView listView;
	private List<FirstItem> firstList;
	private ExpandableListAdapter eAdpater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		listView = (ExpandableListView) findViewById(R.id.expandList);
		firstList = new ArrayList<FirstItem>();
	}

	private void initData() {
		// TODO Auto-generated method stub
		for (int i = 0; i < 10; i++) {
			FirstItem firstItem = new FirstItem();
			firstItem.setId(i);
			firstItem.setTitle("这是第" + i + "个");
			List<SecondItem> seList = new ArrayList<SecondItem>();
			for (int j = i; j < 10; j++) {
				SecondItem secondItem = new SecondItem();
				secondItem.setId(i);
				secondItem.setTitle("子的第" + j * 78 + "条");
				seList.add(secondItem);
				List<ThirdItem> thirdList = new ArrayList<ThirdItem>();
				for (int k = 0; k < j + 1; k++) {
					ThirdItem thirdItem = new ThirdItem();
					thirdItem.setId(k);
					thirdItem.setImage("sss");
					thirdItem.setName("张凯强" + k + j);
					thirdItem.setTel("10086" + j + k);
					thirdList.add(thirdItem);
				}
				secondItem.setThirdItems(thirdList);
			}
			firstItem.setSecondItems(seList);
			firstList.add(firstItem);
		}
		eAdpater = new ExpandAdapter(MainActivity.this, firstList,stvClickEvent);
		listView.setAdapter(eAdpater);
		listView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View view,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this,
						childPosition + "---ccc===" + groupPosition,
						Toast.LENGTH_LONG).show();
				return false;
			}
		});
	}

	OnChildClickListener stvClickEvent = new OnChildClickListener() {

		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			// TODO Auto-generated method stub
			String msg = "parent_id = " + groupPosition + " child_id = "
					+ childPosition;
			Toast.makeText(MainActivity.this, msg,
					Toast.LENGTH_SHORT).show();
			return false;
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

两个Adapter:

package com.zkq.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

import com.example.teltest.R;
import com.zkq.model.FirstItem;
import com.zkq.model.SecondItem;
import com.zkq.util.Constants;

public class ExpandAdapter extends BaseExpandableListAdapter {

	private Context context;
	private List<FirstItem> firstList;
	private OnChildClickListener stvClickEvent;//外部回调函数
	// private List<SecondItem> secondList;

	public ExpandAdapter(Context context, List<FirstItem> firstList,OnChildClickListener stvClickEvent) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.firstList = firstList;
		this.stvClickEvent=stvClickEvent;
		// this.secondList = secondList;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return firstList.get(groupPosition).getSecondItems();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ExpandableListView treeView = getExpandableListView(firstList.get(groupPosition).getSecondItems().size());
		ExpandAdapter2 expandAdapter2 = new ExpandAdapter2(context, firstList,
				childPosition, groupPosition,treeView,stvClickEvent);
		//treeView.setOnChildClickListener(stvClickEvent);
		treeView.setAdapter(expandAdapter2);
		return treeView;
	}

	public ExpandableListView getExpandableListView(int position) {
		  AbsListView.LayoutParams params = new AbsListView.LayoutParams(
	                ViewGroup.LayoutParams.FILL_PARENT, Constants.SECOND_ITEM_HEIGHT*position);
		ExpandableListView superTreeView = new ExpandableListView(context);
		superTreeView.setLayoutParams(params);
		return superTreeView;
	}

	@Override
	public int getChildrenCount(int position) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public Object getGroup(int position) {
		// TODO Auto-generated method stub
		return firstList.get(position);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return firstList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int position, boolean flag, View convertView,
			ViewGroup group) {
		// TODO Auto-generated method stub
		FirstHolder holder = null;
		if (convertView == null) {
			holder = new FirstHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.first_lay, null);
			holder.first_arrow = (ImageView) convertView
					.findViewById(R.id.first_arrow);
			holder.first_image = (ImageView) convertView
					.findViewById(R.id.first_image);
			holder.first_title = (TextView) convertView
					.findViewById(R.id.first_title);
			convertView.setTag(holder);
		} else {
			holder = (FirstHolder) convertView.getTag();
		}
		FirstItem firstItem = firstList.get(position);
		holder.first_title.setText(firstItem.getTitle());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}
}

class FirstHolder {
	TextView first_title;
	ImageView first_image;
	ImageView first_arrow;
}

class SecondHolder {
	TextView second_title;
	ImageView second_arrow;
}
package com.zkq.adapter;

import java.util.List;

import com.example.teltest.R;
import com.zkq.activity.MainActivity;
import com.zkq.model.FirstItem;
import com.zkq.model.SecondItem;
import com.zkq.model.ThirdItem;
import com.zkq.util.Constants;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.AbsListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandAdapter2 extends BaseExpandableListAdapter {

	private Context context;
	private List<FirstItem> firstList;

	private int cpostion;
	private int gposition;
	private ExpandableListView treeView;
	private OnChildClickListener stvClickEvent;//外部回调函数
	private int secondlength;

	// private List<SecondItem> secondList;

	public ExpandAdapter2(Context context, List<FirstItem> firstList,
			int cposition, int gposition, ExpandableListView treeView,OnChildClickListener stvClickEvent) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.firstList = firstList;
		this.cpostion = cposition;
		this.gposition = gposition;
		this.treeView = treeView;
		this.stvClickEvent=stvClickEvent;
		// this.secondList = secondList;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return firstList.get(gposition).getSecondItems().get(groupPosition)
				.getThirdItems().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ThirdViewHolder childViewHolder = null;
		if (convertView == null) {
			childViewHolder = new ThirdViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.third_lay, null);
			childViewHolder.third_name = (TextView) convertView
					.findViewById(R.id.third_name);
			childViewHolder.third_image = (ImageView) convertView
					.findViewById(R.id.third_image);
			childViewHolder.third_tel = (TextView) convertView
					.findViewById(R.id.third_tel);
			convertView.setTag(childViewHolder);
		} else {
			childViewHolder = (ThirdViewHolder) convertView.getTag();
		}
		ThirdItem thirdItem = firstList.get(gposition).getSecondItems()
				.get(groupPosition).getThirdItems().get(childPosition);
		childViewHolder.third_name.setText(thirdItem.getName());
		childViewHolder.third_tel.setText(thirdItem.getTel());
		//获取选中的内容
		treeView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView arg0, View arg1, int groupPosition,
					int childPosition, long arg4) {
				// TODO Auto-generated method stub
				String msg = "-ppp--"+gposition+"parent_id = " + groupPosition + " child_id = "
						+ childPosition;
				Toast.makeText(context, msg,
						Toast.LENGTH_SHORT).show();
				return false;
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int position) {
		// TODO Auto-generated method stub
		return firstList.get(gposition).getSecondItems().get(position)
				.getThirdItems().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return firstList.get(gposition).getSecondItems().get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return firstList.get(gposition).getSecondItems().size();
	}

	@Override
	public long getGroupId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getGroupView(int position, boolean arg1, View convertView,
			ViewGroup arg3) {
		SecondHolder childViewHolder = null;
		if (convertView == null) {
			childViewHolder = new SecondHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.second_lay, null);
			childViewHolder.second_title = (TextView) convertView
					.findViewById(R.id.second_title);
			convertView.setTag(childViewHolder);
		} else {
			childViewHolder = (SecondHolder) convertView.getTag();
		}
		SecondItem secondItem = firstList.get(gposition).getSecondItems()
				.get(position);
		childViewHolder.second_title.setText(secondItem.getTitle());
		/***
		 * 展开监听
		 */
		treeView.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int position) {
				// TODO Auto-generated method stub

				if (treeView.getChildCount() == firstList.get(gposition)
						.getSecondItems().size()) {
					if (secondlength > 0) {
						secondlength += firstList.get(gposition)
								.getSecondItems().get(position).getThirdItems()
								.size()
								* Constants.THIRD_ITEM_HEIGHT;
					} else {
						secondlength += firstList.get(gposition)
								.getSecondItems().size()
								* Constants.SECOND_ITEM_HEIGHT
								+ firstList.get(gposition).getSecondItems()
										.get(position).getThirdItems().size()
								* Constants.THIRD_ITEM_HEIGHT;
					}
				} else {
					secondlength += firstList.get(gposition).getSecondItems()
							.get(position).getThirdItems().size()
							* Constants.THIRD_ITEM_HEIGHT;
				}
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT, secondlength);
				treeView.setLayoutParams(lp);
			}
		});
		// treeView.setOnGroupClickListener(new OnGroupClickListener() {
		// @Override
		// public boolean onGroupClick(ExpandableListView arg0, View arg1,
		// int position, long arg3) {
		// // TODO Auto-generated method stub
		// return false;
		// }
		// });
		/***
		 * 缩放监听
		 */
		treeView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int position) {
				// TODO Auto-generated method stub

				secondlength -= firstList.get(gposition).getSecondItems()
						.get(position).getThirdItems().size()
						* Constants.THIRD_ITEM_HEIGHT;
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT, secondlength);
				treeView.setLayoutParams(lp);
			}
		});
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	class ThirdViewHolder {
		ImageView third_image;
		TextView third_name;
		TextView third_tel;
	}

	class SecondViewHolder {
		TextView second_title;
		ImageView second_arrow;
	}

}

下面时三个实体类:

package com.zkq.model;

import java.util.List;

public class FirstItem {
	private int id;
	private String title;
	private String image;
	private List<SecondItem> secondItems;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public List<SecondItem> getSecondItems() {
		return secondItems;
	}

	public void setSecondItems(List<SecondItem> secondItems) {
		this.secondItems = secondItems;
	}

}
package com.zkq.model;

import java.util.List;

public class SecondItem {
	private int id;
	private String title;
	private List<ThirdItem> thirdItems;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<ThirdItem> getThirdItems() {
		return thirdItems;
	}

	public void setThirdItems(List<ThirdItem> thirdItems) {
		this.thirdItems = thirdItems;
	}

}
package com.zkq.model;

public class ThirdItem {
	private int id;
	private String name;
	private String tel;
	private String image;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getImage() {
		return image;
	}
	public void setImage(String image) {
		this.image = image;
	}

}

常量

package com.zkq.util;

public class Constants {
	/**
	 * 第二级和第三级菜单的高度
	 */
	public static final int SECOND_ITEM_HEIGHT=115;
	public static final int THIRD_ITEM_HEIGHT=120;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 00:18:13

android 三级菜单 BaseExpandableListAdapter的相关文章

android ExpandableListView三级菜单的使用

由于本人所作的项目需要用到这种列表式的收缩与展开,因此,就好好研究了有关这方面的一些知识,当然,也借鉴了网上一些成功的案列.下面就是我模拟测试的一个展示界面. 实现上面的这些功能,我主要是通过ExpandableListView这一控件,以及BaseExpandableListAdapter适配器.这两者关联实现的.好的,那接下来,就对这些进行详细的展示. 所有的xml布局展示 ## activity_main.xml## <?xml version="1.0" encoding

Android自己定义控件:老版优酷的三级菜单(效果图 + Demo)

效果图: 制作思路: 1.先分析这个效果,事实上能够理解为把三级菜单分成level1,level2,level3,level1是始终显示的. 点击level1后,level2会出现:点击level2后,level3会出现:level2,level3出现后,点击level1,level2和level3都会消失. 然后消失和出现我们用到了一个动画效果. 2.动画效果用到的是RotateAnimation.因为我们都是用同一个效果,那么我们仅仅要写一个类,把效果实现了就能够了.要是使用RotateAn

Android自定义控件:老版优酷的三级菜单(效果图 + Demo)

效果图: 制作思路: 1.先分析这个效果,其实可以理解为把三级菜单分成 level1,level2,level3,level1是始终显示的.点击level1后,level2会出现:点击level2后,level3会出 现:level2,level3出现后,点击level1,level2和level3都会消失.然后消失和出现我们用到了一个动画效果. 2.动画效果用到的是RotateAnimation,由于我们都是用同一个效果,那么我们只要写一个类,把效果实现了就可以了.要是使用RotateAnim

Android--简单的三级菜单

  关于这个菜单应该在很多播放器应用里面可以看见,直接先上两张效果图吧,一张是该Demo的效果图,一张是优酷手机客户端的效果图.                                                         DEMO的效果图                                                                   优酷手机客户端界面 因为没有时间去自己制作图标,所以Demo里面采用的就是优酷手机客户端里的图标了. 一.布

Python学习笔记五:字符串常用操作,字典,三级菜单实例

字符串常用操作 7月19日,7月20日 ,7月22日,7月29日,8月29日,2月29日 首字母大写:a_str.capitalize() 统计字符串个数:a_str.count("x") 输出字符,不够的使用指定的字符补上,字符居中:a_str.center(50,"-") 判断字符串以什么结尾:a_str.endwith("xx") 将字符串中的tab转换为指定数目的空格:a_str.expandtabs(tabsize=30) 查找指定字符

BottomBar之Android底部菜单

BottomBar之Android底部菜单 前言:开源项目BottomBar,实现Android底部菜单(常用菜单,BottomBar实现动画(上下式)+消息菜单,BottomBar+ViewPager+Fragment实现炫酷的底部导航效果) 效果: 开发环境:AndroidStudio2.2.1+gradle-2.14.1 引入依赖: compile 'com.android.support:appcompat-v7:23.0.0' compile 'com.android.support:

三级菜单制作

HTML代码: <body> <div class="navMenu"> <ul> <li><a href="#">首页</a> <ul> <li><a href="#">JavaScript+</a> <ul> <li><a href="#">三角函数</a>

Python 三级菜单

Python 三级菜单 需求 打印省.市.县三级菜单 可返回上一级 可随时退出程序 代码实现 1 menu = { 2 '北京':{ 3 '海淀':{ 4 '五道口':{ 5 'soho':{}, 6 '网易':{}, 7 'google':{} 8 }, 9 '中关村':{ 10 '爱奇艺':{}, 11 '汽车之家':{}, 12 'youku':{}, 13 }, 14 '上地':{ 15 '百度':{}, 16 }, 17 }, 18 '昌平':{ 19 '沙河':{ 20 '老男孩':

新手小白 python之路 Day1 (三级菜单功能实现)

直接上需求: 实现一个多级菜单 三级菜单的实现 可以依次选择进入各个子菜单 也可以返回上级菜单 所需知识 列表 字典 需求也比较简单,但实际上做起来还是遇到许多的问题,我这边主要用到的知识点大概就是  字典 等 大致的思路 我是分别写了四个函数 Abnormal() 通过抛出异常来判断输入的编号是否合法 Province() 获取省级菜单 City() 获取市级菜单 Area() 获取区级菜单 来实现整个功能的,话不多说直接上代码了 #!/usr/bin/env python # -*- cod