android 二级菜单实现
2016-09-26 13:30
344 查看
xml文件
java文件
主文件
两个适配器文件
1、
2、
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.hls.json.ButtonActivity" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_height="wrap_content" android:text="两层结构" android:layout_width="160dip" android:id="@+id/btnNormal"> </Button> <Button android:layout_height="wrap_content" android:text="三层结构" android:layout_width="160dip" android:id="@+id/btnSuper"></Button> </LinearLayout> <ExpandableListView android:id="@+id/ExpandableListView01" android:layout_width="fill_parent" android:layout_height="fill_parent"> </ExpandableListView> </LinearLayout>
java文件
主文件
package com.example.hls.json; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ExpandableListView; import android.widget.Toast; import java.util.List; public class ButtonActivity extends Activity { /** Called when the activity is first created. */ ExpandableListView expandableList; TreeViewAdapter adapter; SuperTreeViewAdapter superAdapter; Button btnNormal,btnSuper; // Sample data set. children[i] contains the children (String[]) for groups[i]. public String[] groups = { "xxxx好友", "xxxx同学", "xxxxx女人"}; public String[][] child= { { "A君", "B君", "C君", "D君" }, { "同学甲", "同学乙", "同学丙"}, { "御姐", "萝莉" } }; public String[] parent = { "xxxx好友", "xxxx同学"}; public String[][][] child_grandson= { {{"A君"}, {"AA","AAA"}}, {{"B君"}, {"BBB","BBBB","BBBBB"}}, {{"C君"}, {"CCC","CCCC"}}, {{"D君"}, {"DDD","DDDD","DDDDD"}}, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); // this.setTitle("ExpandableListView练习----hellogv"); btnNormal=(Button)this.findViewById(R.id.btnNormal); btnNormal.setOnClickListener(new ClickEvent()); btnSuper=(Button)this.findViewById(R.id.btnSuper); btnSuper.setOnClickListener(new ClickEvent()); adapter=new TreeViewAdapter(this,TreeViewAdapter.PaddingLeft>>1); superAdapter=new SuperTreeViewAdapter(this,stvClickEvent); expandableList=(ExpandableListView) ButtonActivity.this.findViewById(R.id.ExpandableListView01); } class ClickEvent implements View.OnClickListener{ @Override public void onClick(View v) { adapter.RemoveAll(); adapter.notifyDataSetChanged(); superAdapter.RemoveAll(); superAdapter.notifyDataSetChanged(); if(v==btnNormal) { List<TreeViewAdapter.TreeNode> treeNode = adapter.GetTreeNode(); for(int i=0;i<groups.length;i++) { TreeViewAdapter.TreeNode node=new TreeViewAdapter.TreeNode(); node.parent=groups[i]; for(int ii=0;ii<child[i].length;ii++) { node.childs.add(child[i][ii]); } treeNode.add(node); } adapter.UpdateTreeNode(treeNode); expandableList.setAdapter(adapter); expandableList.setOnChildClickListener(new ExpandableListView.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(ButtonActivity.this, str, Toast.LENGTH_SHORT).show( 10eb1 ); return false; } }); } else if(v==btnSuper){ List<SuperTreeViewAdapter.SuperTreeNode> superTreeNode = superAdapter.GetTreeNode(); for(int i=0;i<parent.length;i++)//第一层 { SuperTreeViewAdapter.SuperTreeNode superNode=new SuperTreeViewAdapter.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); expandableList.setAdapter(superAdapter); } } } /** 三级树形菜单的事件不再可用,本函数由三级树形菜单的子项(二级菜单)进行回调 */ ExpandableListView.OnChildClickListener stvClickEvent=new ExpandableListView.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(ButtonActivity.this, str, Toast.LENGTH_SHORT).show(); return false; } }; }
两个适配器文件
1、
package com.example.hls.json; 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.TextView; import java.util.ArrayList; import java.util.List; /** * Created by HLS on 2016/9/26. */ 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 ExpandableListView.OnChildClickListener stvClickEvent;//外部回调函数 public SuperTreeViewAdapter(Context view,ExpandableListView.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<TreeViewAdapter.TreeNode> tmp = treeViewAdapter.GetTreeNode();//临时变量取得TreeViewAdapter的TreeNode集合,可为空 final TreeViewAdapter.TreeNode treeNode=(TreeViewAdapter.TreeNode) getChild(groupPosition, childPosition); tmp.add(treeNode); treeViewAdapter.UpdateTreeNode(tmp); treeView.setAdapter(treeViewAdapter); //关键点:取得选中的二级树形菜单的父子节点,结果返回给外部回调函数 treeView.setOnChildClickListener(this.stvClickEvent); /** 关键点:第二级菜单展开时通过取得节点数来设置第三级菜单的大小 */ treeView.setOnGroupExpandListener(new ExpandableListView.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 ExpandableListView.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; } }
2、
package com.example.hls.json; 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; import java.util.ArrayList; import java.util.List; /** * Created by HLS on 2016/9/26. */ public class TreeViewAdapter extends BaseExpandableListAdapter{ public static final int ItemHeight = 48;//每项的高度 public static final int PaddingLeft = 36;//每项的高度 private int myPaddingLeft = 0;//如果是由SuperTreeView调用,则作为子项需要往右移 static public class TreeNode { Object parent; List<Object> childs = new ArrayList<Object>(); } 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; } }
相关文章推荐
- Android 二级串联菜单的实现过程
- Android横向二级联动菜单的实现
- Android实现横向二级菜单
- Android 使用BRVAH 实现二级菜单。类似QQ分组
- android 二级菜单的实现
- Android 自定义HorizontalScrollView实现二级菜单
- Android横向二级联动菜单的实现
- Android中实现二级菜单
- android实现底部导航栏和顶部导航栏(相当于网页上的一级菜单和二级菜单)
- 数据库 + TreeView 实现二级菜单
- smarty + ajax 实现二级动态级联菜单
- JavaScript实际应用:简单二级联动菜单实现
- mm_menu.js(实现鼠标移入横向显示二级菜单效果)(2)
- mm_menu.js(实现鼠标移入横向显示二级菜单效果)(1)
- 二级菜单实现
- 用php+javascript实现二级级联菜单的制作
- android简单demo学习系例之菜单实现
- ASP.NET利用JavaScript实现无刷新闪烁二级联动菜单
- ASP.NET利用JavaScript实现无刷新闪烁二级联动菜单
- JavaScript实际应用:简单二级联动菜单实现