zoukankan      html  css  js  c++  java
  • 在android里用ExpandableListView实现二层和三层列表

    转载自http://www.cnblogs.com/nuliniaoboke/archive/2012/11/13/2767957.html

    二层列表是直接用androidAPI中的ExpandableListView即可实现,三层列表其实是对二层列表的一个嵌套,实现起来会相对繁琐。

    部分代码:

      1 public class SuperTreeViewAdapter extends BaseExpandableListAdapter {
      2 
      3     static public class SuperTreeNode {
      4         Object parent;
      5         //二级树形菜单的结构体
      6         List<TreeViewAdapter.TreeNode> childs = new ArrayList<TreeViewAdapter.TreeNode>();
      7     }
      8 
      9     private List<SuperTreeNode> superTreeNodes = new ArrayList<SuperTreeNode>();
     10     private Context parentContext;
     11     private OnChildClickListener stvClickEvent;//外部回调函数
     12     
     13     public SuperTreeViewAdapter(Context view,OnChildClickListener stvClickEvent) {
     14         parentContext = view;
     15         this.stvClickEvent=stvClickEvent;
     16     }
     17 
     18     public List<SuperTreeNode> GetTreeNode() {
     19         return superTreeNodes;
     20     }
     21 
     22     public void UpdateTreeNode(List<SuperTreeNode> node) {
     23         superTreeNodes = node;
     24     }
     25     
     26     public void RemoveAll()
     27     {
     28         superTreeNodes.clear();
     29     }
     30     
     31     public Object getChild(int groupPosition, int childPosition) {
     32         return superTreeNodes.get(groupPosition).childs.get(childPosition);
     33     }
     34 
     35     public int getChildrenCount(int groupPosition) {
     36         return superTreeNodes.get(groupPosition).childs.size();
     37     }
     38 
     39     public ExpandableListView getExpandableListView() {
     40         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
     41                 ViewGroup.LayoutParams.FILL_PARENT, TreeViewAdapter.ItemHeight);
     42         ExpandableListView superTreeView = new ExpandableListView(parentContext);
     43         superTreeView.setLayoutParams(lp);
     44         return superTreeView;
     45     }
     46 
     47     /**
     48      * 三层树结构中的第二层是一个ExpandableListView
     49      */    
     50     public View getChildView(int groupPosition, int childPosition,
     51             boolean isLastChild, View convertView, ViewGroup parent) {
     52         //
     53         final ExpandableListView treeView = getExpandableListView();
     54         final TreeViewAdapter treeViewAdapter = new TreeViewAdapter(this.parentContext,0);
     55         List<TreeNode> tmp = treeViewAdapter.getTreeNode();//临时变量取得TreeViewAdapter的TreeNode集合,可为空
     56         final TreeNode treeNode=(TreeNode) getChild(groupPosition, childPosition);
     57         tmp.add(treeNode);
     58         treeViewAdapter.updateTreeNode(tmp);
     59         treeView.setAdapter(treeViewAdapter);
     60         
     61         //关键点:取得选中的二级树形菜单的父子节点,结果返回给外部回调函数
     62         treeView.setOnChildClickListener(this.stvClickEvent);
     63         
     64         /**
     65          * 关键点:第二级菜单展开时通过取得节点数来设置第三级菜单的大小
     66          */
     67         treeView.setOnGroupExpandListener(new OnGroupExpandListener() {
     68             @Override
     69             public void onGroupExpand(int groupPosition) {
     70                 
     71                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
     72                         ViewGroup.LayoutParams.FILL_PARENT,
     73                         (treeNode.childs.size()+1)*TreeViewAdapter.ItemHeight + 10);
     74                 treeView.setLayoutParams(lp);
     75             }
     76         });
     77         
     78         /**
     79          * 第二级菜单回收时设置为标准Item大小
     80          */
     81         treeView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
     82             @Override
     83             public void onGroupCollapse(int groupPosition) {
     84                 
     85                 AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
     86                         TreeViewAdapter.ItemHeight);
     87                 treeView.setLayoutParams(lp);
     88             }
     89         });
     90         treeView.setPadding(TreeViewAdapter.PaddingLeft*2, 0, 0, 0);
     91         return treeView;
     92     }
     93 
     94     /**
     95      * 三级树结构中的首层是TextView,用于作为title
     96      */
     97     public View getGroupView(int groupPosition, boolean isExpanded,
     98             View convertView, ViewGroup parent) {
     99         TextView textView = TreeViewAdapter.getTextView(this.parentContext);
    100         textView.setText(getGroup(groupPosition).toString());
    101         textView.setPadding(TreeViewAdapter.PaddingLeft*2, 0, 0, 0);
    102         return textView;
    103     }
    104 
    105     public long getChildId(int groupPosition, int childPosition) {
    106         return childPosition;
    107     }
    108 
    109     public Object getGroup(int groupPosition) {
    110         return superTreeNodes.get(groupPosition).parent;
    111     }
    112 
    113     public int getGroupCount() {
    114         return superTreeNodes.size();
    115     }
    116 
    117     public long getGroupId(int groupPosition) {
    118         return groupPosition;
    119     }
    120 
    121     public boolean isChildSelectable(int groupPosition, int childPosition) {
    122         return true;
    123     }
    124 
    125     public boolean hasStableIds() {
    126         return true;
    127     }
    128 }
    129 
    130 SuperTreeViewAdapter.java
    SuperTreeViewAdapter.java
     1 List<SuperTreeViewAdapter.SuperTreeNode> superNodeTree = superAdapter.GetTreeNode();
     2                 forint i = 0; i < parent.length; i++) {
     3                     SuperTreeViewAdapter.SuperTreeNode superNode = new SuperTreeViewAdapter.SuperTreeNode();
     4                     superNode.parent = parent[i];
     5     forint j = 0; j < child_grandchild.length; j++) {
     6                     
     7     TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();
     8             
     9             node.parent = child_grandchild[j][0][0];
    10                
    11         forint k = 0; k < child_grandchild[j][1].length; k++) {
    12                           node.childs.add(child_grandchild[j][1][k]);
    13                         }
    14                         
    15   superNode.childs.add(node);
    16             
    17           }
    18                       superNodeTree.add(superNode);
    19                   }        
    20   superAdapter.UpdateTreeNode(superNodeTree);
    21                 
    22 expandableListView.setAdapter(superAdapter);
    23 
    24 ExpandableListViewActivity.java
    ExpandableListViewActivity.java
     1 <LinearLayout
     2         android:id="@+id/linearLayout1"
     3         android:layout_width="match_parent"
     4         android:layout_height="wrap_content"
     5         android:layout_gravity="center"
     6         android:gravity="center" >
     7 
     8         <Button
     9             android:id="@+id/button1"
    10             android:layout_width="100dip"
    11             android:layout_height="wrap_content"
    12             android:text="二层结构" />
    13 
    14         <Button
    15             android:id="@+id/button2"
    16             android:layout_width="100dip"
    17             android:layout_height="wrap_content"
    18             android:text="三层结构" />
    19     </LinearLayout>
    20 
    21     <ExpandableListView
    22         android:id="@+id/expandablelistview"
    23         android:layout_width="fill_parent"
    24         android:layout_height="fill_parent"
    25         >
    26     </ExpandableListView>
    27 
    28 main.xml
    main.xml

    效果图 :

    二层列表

    三层列表

  • 相关阅读:
    【读书笔记】程序员的自我修养总结(三)
    【DSP开发】利用CCS5.4开发基于DSP6455的JPEG2000图像解压缩过程
    【DSP开发】利用CCS5.4开发基于DSP6455的JPEG2000图像解压缩过程
    【读书笔记】程序员的自我修养总结(二)
    【读书笔记】程序员的自我修养总结(二)
    【读书笔记】程序员的自我修养总结(一)
    【读书笔记】程序员的自我修养总结(一)
    CMake生成VS2010工程相对路径和绝对路径问题说明
    CMake生成VS2010工程相对路径和绝对路径问题说明
    关于lib和dll
  • 原文地址:https://www.cnblogs.com/vinozly/p/4766722.html
Copyright © 2011-2022 走看看