Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下。

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:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/btn_tree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="两层结构" /> <Button
android:id="@+id/btn_supertree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="三层结构" /> </LinearLayout> <ExpandableListView
android:id="@+id/expandlist1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView> </LinearLayout>

接下来增加两个类TreeViewAdapter,SuperTreeViewAdaper继承BaseExpandableListAdapter,实现两层结构和三层结构效果:

TreeViewAdapter代码:

 package com.example.expandlistdemo;

 import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
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 TreeViewAdapter extends BaseExpandableListAdapter { public static final int ItemHeight = 48;
public static final int PaddingLeft = 36;
private int myPaddingLeft = 0; static public class TreeNode {
Object parent;
List childs = new ArrayList();
} List<TreeNode> treeNodes = new ArrayList<TreeNode>();
Context parentContext; public TreeViewAdapter(Context view, int myPaddingLeft) {
parentContext = view;
this.myPaddingLeft = myPaddingLeft;
} public List<TreeNode> GetTreeNode() {
return treeNodes;
} public void UpdateTreeNode(List<TreeNode> nodes) {
treeNodes = nodes;
} public void RemoveAll() {
treeNodes.clear();
} public Object getChild(int groupPosition, int childPosition) {
return treeNodes.get(groupPosition).childs.get(childPosition);
} public int getChildrenCount(int groupPosition) {
return treeNodes.get(groupPosition).childs.size();
} static public TextView getTextView(Context context) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ItemHeight); TextView textView = new TextView(context);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
return textView;
} public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView(this.parentContext);
textView.setText(getChild(groupPosition, childPosition).toString());
textView.setPadding(myPaddingLeft + PaddingLeft, 0, 0, 0);
return textView;
} public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getTextView(this.parentContext);
textView.setText(getGroup(groupPosition).toString());
textView.setPadding(myPaddingLeft + (PaddingLeft >> 1), 0, 0, 0);
return textView;
} public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} public Object getGroup(int groupPosition) {
return treeNodes.get(groupPosition).parent;
} public int getGroupCount() {
return treeNodes.size();
} public long getGroupId(int groupPosition) {
return groupPosition;
} public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
} public boolean hasStableIds() {
return true;
} }

SuperTreeViewAdaper代码:

 package com.example.expandlistdemo;

 import java.util.ArrayList;
import java.util.List;
import com.example.expandlistdemo.TreeViewAdapter.TreeNode;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView; public class SuperTreeViewAdaper extends BaseExpandableListAdapter { static public class SuperTreeNode {
Object parent;
// 二级树形菜单的结构体
List<TreeNode> childs = new ArrayList<TreeNode>();
} private List<SuperTreeNode> superTreeNodes = new ArrayList<SuperTreeNode>();
private Context parentContext;
private OnChildClickListener stvClickEvent;// 外部回调函数 public SuperTreeViewAdaper(Context view, OnChildClickListener stvClickEvent) {
parentContext = view;
this.stvClickEvent = stvClickEvent;
} public List<SuperTreeNode> GetTreeNode() {
return superTreeNodes;
} public void UpdateTreeNode(List<SuperTreeNode> node) {
superTreeNodes = node;
} public void RemoveAll() {
superTreeNodes.clear();
} public Object getChild(int groupPosition, int childPosition) {
return superTreeNodes.get(groupPosition).childs.get(childPosition);
} public int getChildrenCount(int groupPosition) {
return superTreeNodes.get(groupPosition).childs.size();
} public ExpandableListView getExpandableListView() {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, TreeViewAdapter.ItemHeight);
ExpandableListView superTreeView = new ExpandableListView(parentContext);
superTreeView.setLayoutParams(lp);
return superTreeView;
} /**
* 三层树结构中的第二层是一个ExpandableListView
*/
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// 是
final ExpandableListView treeView = getExpandableListView();
final TreeViewAdapter treeViewAdapter = new TreeViewAdapter(
this.parentContext, 0);
List<TreeNode> tmp = treeViewAdapter.GetTreeNode();// 临时变量取得TreeViewAdapter的TreeNode集合,可为空
final TreeNode treeNode = (TreeNode) getChild(groupPosition,
childPosition);
tmp.add(treeNode);
treeViewAdapter.UpdateTreeNode(tmp);
treeView.setAdapter(treeViewAdapter); // 关键点:取得选中的二级树形菜单的父子节点,结果返回给外部回调函数
treeView.setOnChildClickListener(this.stvClickEvent); /**
* 关键点:第二级菜单展开时通过取得节点数来设置第三级菜单的大小
*/
treeView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) { AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, (treeNode.childs
.size() + 1) * TreeViewAdapter.ItemHeight + 10);
treeView.setLayoutParams(lp);
}
}); /**
* 第二级菜单回收时设置为标准Item大小
*/
treeView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) { AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
TreeViewAdapter.ItemHeight);
treeView.setLayoutParams(lp);
}
});
treeView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);
return treeView;
} /**
* 三级树结构中的首层是TextView,用于作为title
*/
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = TreeViewAdapter.getTextView(this.parentContext);
textView.setText(getGroup(groupPosition).toString());
textView.setPadding(TreeViewAdapter.PaddingLeft, 0, 0, 0);
return textView;
} public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} public Object getGroup(int groupPosition) {
return superTreeNodes.get(groupPosition).parent;
} public int getGroupCount() {
return superTreeNodes.size();
} public long getGroupId(int groupPosition) {
return groupPosition;
} public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
} public boolean hasStableIds() {
return true;
}
}

修改下MainActivity.java:

 package com.example.expandlistdemo;

 import java.util.List;

 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast; public class MainActivity extends Activity { private Button btnTree;
private Button btnSuperTree;
private ExpandableListView expandlist;
private TreeViewAdapter adapter;
private SuperTreeViewAdaper superAdapter; public String[] groupTree = { "老师", "学生", "朋友" };
public String[][] child = { { "王老师", "陈老师", "李老师", "黄老师" },
{ "学生甲", "学生乙", "学生丙" }, { "朋友甲", "朋友丁" } }; public String[] parent = { "老师", "学生" };
public String[][][] child_grandson = { { { "王老师" }, { "AA", "AAA" } },
{ { "陈老师" }, { "BBB", "BBBB", "BBBBB" } },
{ { "李老师" }, { "CCC", "CCCC" } },
{ { "黄老师" }, { "DDD", "DDDD", "DDDDD" } } }; /**
* 三级树形菜单的事件不再可用,本函数由三级树形菜单的子项(二级菜单)进行回调
*/
OnChildClickListener stvClickEvent = new OnChildClickListener() { @Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) { String str = "parent id:" + String.valueOf(groupPosition)
+ ",children id:" + String.valueOf(childPosition);
Toast.makeText(getApplicationContext(), str, 300).show(); return false; } }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnTree = (Button) findViewById(R.id.btn_tree);
btnSuperTree = (Button) findViewById(R.id.btn_supertree); adapter = new TreeViewAdapter(this, TreeViewAdapter.PaddingLeft >> 1); superAdapter = new SuperTreeViewAdaper(this, stvClickEvent);
expandlist = (ExpandableListView) findViewById(R.id.expandlist1); btnTree.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { adapter.RemoveAll();
adapter.notifyDataSetChanged();
superAdapter.RemoveAll();
superAdapter.notifyDataSetChanged(); List treeNode = adapter.GetTreeNode();
for (int i = 0; i < groupTree.length; i++) {
TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();
node.parent = groupTree[i];
for (int ii = 0; ii < child[i].length; ii++) {
node.childs.add(child[i][ii]);
}
treeNode.add(node);
} adapter.UpdateTreeNode(treeNode);
expandlist.setAdapter(adapter);
expandlist.setOnChildClickListener(new OnChildClickListener() { @Override
public boolean onChildClick(ExpandableListView arg0,
View arg1, int parent, int children, long arg4) { String str = "parent id:" + String.valueOf(parent)
+ ",children id:" + String.valueOf(children);
Toast.makeText(getApplicationContext(), str, 300)
.show();
return false;
}
}); }
}); btnSuperTree.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { adapter.RemoveAll();
adapter.notifyDataSetChanged();
superAdapter.RemoveAll();
superAdapter.notifyDataSetChanged(); List superTreeNode = superAdapter.GetTreeNode();
for (int i = 0; i < parent.length; i++) // 第一层
{
SuperTreeViewAdaper.SuperTreeNode superNode = new SuperTreeViewAdaper.SuperTreeNode();
superNode.parent = parent[i]; // 第二层
for (int ii = 0; ii < child_grandson.length; ii++) {
TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();
node.parent = child_grandson[ii][0][0];// 第二级菜单的标题 for (int iii = 0; iii < child_grandson[ii][1].length; iii++) // 第三级菜单
{
node.childs.add(child_grandson[ii][1][iii]);
}
superNode.childs.add(node);
}
superTreeNode.add(superNode); }
superAdapter.UpdateTreeNode(superTreeNode);
expandlist.setAdapter(superAdapter);
} });
}
}

运行效果:

点击两层结构:

     

点击三层结构:

    

22.Android之ExpandableListView树形列表学习的更多相关文章

  1. Android开发 ExpandableListView 可折叠列表详解

    前言 在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListView. 其实这个View的原始设计还是ListView的那套.就是增加2层的ListView而已 ...

  2. android 实现QQ好友列表

    在某些Android开发群里,看到有些新手问怎么实现QQ好友列表,其实网上一搜挺多的.接触Android,也才一年的时间,大部分时间花在工作上(解bug...),界面上开发很少参与.自己维护的系统应用 ...

  3. Winform开发主界面菜单的动态树形列表展示

    我在之前很多文章里面,介绍过Winform主界面的开发,基本上都是标准的界面,在顶部放置工具栏,中间区域则放置多文档的内容,但是在顶部菜单比较多的时候,就需要把菜单分为几级处理,如可以在顶部菜单放置一 ...

  4. Android之ExpandableListView

    ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...

  5. android的helloworld工程目录学习

    android的helloworld工程目录学习 Android工程的主要目录有src.gen.Android X.X.bin.res等文件夹. 1.     Src文件夹 Src文件夹包含java源 ...

  6. Android - N级树形结构实现

    目前已经实现3级之内的任意级树形结构展示(如果想增加更多级,需要扩展排序算法),并支持单选和多选(使用不同的适配器). 实现使用的控件:ListView 首先,最重要的应该是数据源的格式,支持树形结构 ...

  7. Android 之 ExpandableListView 的使用

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  8. Android中ExpandableListView控件基本使用

    本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...

  9. Android在ExpandableListView控制的基本使用

    在本文中,Demo为了展示Android在ExpandableListView用途管制.如该组/儿子ListView绑定数据源. 直接上代码例如以下: 程序结构图: layout文件夹下的 main. ...

随机推荐

  1. Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列

    题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...

  2. 权限框架 - shiro 授权demo

    之前说了权限认证,其实也就是登录验证身份 这次来说说shiro的授权 shiro可以针对角色授权,或者访问资源授权 两者都行,但是在如今的复杂系统中,当然使用后者,如果你是小系统或者私活的话,前者即可 ...

  3. Unity-WIKI 之 SplashScreen

    组件功能 在屏幕上的一个启动画面消失,等待几秒钟(或等待用户输入),然后淡出,下一个场景加载. 组件源码 using UnityEngine; using System.Collections; // ...

  4. [Unity2D]2D Mobile Joystick

    效果预览 操作步骤 1.下载素材 http://pan.bai du.com/s/1gdkQz8v 2.新建一个GUITexture(Joystick)及一个Sprite(Nyan)   3.添加背景 ...

  5. Saltstack-初体验

    安装 rpm -Uvh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm yum install s ...

  6. python-基础案例

    范例一: 练习:元素分类 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值 ...

  7. 微软职位内部推荐-Software Development Engineering II

    微软近期Open的职位: Job Title: Software Development Engineering II Work Location: Suzhou, China Enterprise ...

  8. web前端开发资源整理

    webpack中文文档 http://webpackdoc.com/index.html 饿了么UI http://mint-ui.github.io/#!/zh-cn http://element. ...

  9. WebGIS空间数据请求访问机制

    通用的WebGIS空间数据请求访问机制如下图所示: 图1 WebGIS空间数据请求访问机制

  10. 逆向最大匹配分词算法C#

    逆向顺序 句子:大家好我叫XX我是一名程序员 程序员 -> 序员 -> 员 名程序 -> 程序 -> 序 一名程 -> 名程 -> 程 是一名 -> 一名 - ...