在android里用ExpandableListView实现二层和三层列表源码

[来源] 达内    [编辑] 达内   [时间]2012-11-13

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

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

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">部分代码:

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">SuperTreeViewAdapter.java

< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(75, 75, 75); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
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
*2, 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*2, 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;     } }
< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">ExpandableListViewActivity.java

< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(75, 75, 75); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
List<SuperTreeViewAdapter.SuperTreeNode> superNodeTree = superAdapter.GetTreeNode();                 
forint
 i = 0; i < parent.length; i++

) {                     SuperTreeViewAdapter.SuperTreeNode superNode = new
 SuperTreeViewAdapter.SuperTreeNode();                     superNode.parent = parent[i];     
forint j = 0; j < child_grandchild.length; j++
) {                          TreeViewAdapter.TreeNode node = new
 TreeViewAdapter.TreeNode();                          node.parent = child_grandchild[j][0][0];                         
forint k = 0; k < child_grandchild[j][1].length; k++
) {                           node.childs.add(child_grandchild[j][1

][k]);                         }                            superNode.childs.add(node);                        }                       superNodeTree.add(superNode);                   }           superAdapter.UpdateTreeNode(superNodeTree);                  expandableListView.setAdapter(superAdapter);     
< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">main.xml

< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; color: rgb(75, 75, 75); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
<LinearLayout         android:id="@+id/linearLayout1"
         android:layout_width="match_parent"         android:layout_height
="wrap_content"

         android:layout_gravity="center"         android:gravity
="center" >

        <Button             android:id="@+id/button1"
             android:layout_width="100dip"             android:layout_height
="wrap_content"

             android:text
="二层结构" />

        <Button             android:id="@+id/button2"
             android:layout_width="100dip"             android:layout_height
="wrap_content"

             android:text
="三层结构" />
    </LinearLayout>

    <ExpandableListView         android:id="@+id/expandablelistview"
         android:layout_width="fill_parent"         android:layout_height
="fill_parent"
        >
    </ExpandableListView>
< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">
效果图 :

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">三层列表

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">二层列表

< p style="line-height: 19px; margin: 10px auto; text-indent: 0px; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">

资源下载