22.Android之ExpandableListView树形列表学习

时间:2022-12-03 15:58:52

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树形列表学习     22.Android之ExpandableListView树形列表学习

点击三层结构:

22.Android之ExpandableListView树形列表学习    22.Android之ExpandableListView树形列表学习