龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android提高之多级树形菜单的实现方法(3)

时间:2014-08-10 02:32来源:网络整理 作者:网络 点击:
分享到:
SuperTreeViewAdapter.java是实现三级树形菜单的工具类,会用到TreeViewAdapter.java,源码如下: package com.testExpandableList;import java.util.ArrayList;import java.util.List;impor

SuperTreeViewAdapter.java是实现三级树形菜单的工具类,会用到TreeViewAdapter.java,源码如下:

package com.testExpandableList;
import java.util.ArrayList;
import java.util.List;
import com.testExpandableList.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 SuperTreeViewAdapter extends BaseExpandableListAdapter {
 static public class SuperTreeNode {
 Object parent;
 //二级树形菜单的结构体
 List<TreeViewAdapter.TreeNode> childs = new ArrayList<TreeViewAdapter.TreeNode>();
 }
 private List<SuperTreeNode> superTreeNodes = new ArrayList<SuperTreeNode>();
 private Context parentContext;
 private OnChildClickListener stvClickEvent;//外部回调函数
 public SuperTreeViewAdapter(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;
 }
}

总结,使用ExpandableList实现三级树形菜单时有些bug不好解决,而且定义三维数组的时候也要倍加小心!所以尽量把数据化简来使用二级树形菜单。

希望本文所述代码对大家进行Android程序设计有一定的帮助。

精彩图集

赞助商链接