Use expandablelistview in Android to implement Layer 2 and Layer 3 list source code

Source: Internet
Author: User

The L2 list can be directly implemented using the expandablelistview in androidapi. the L3 list is actually a nesting of the L2 list, which is relatively cumbersome to implement.

PartCode:

Supertreeviewadapter. Java

 Public   Class Supertreeviewadapter Extends  Baseexpandablelistadapter {  Static   Public   Class  Supertreenode {object parent;  //  Structure of level 2 tree menu List <treeviewadapter. treenode> Childs = New Arraylist <treeviewadapter. treenode> ();}  Private List <supertreenode> supertreenodes = New Arraylist <supertreenode> ();  Private  Context parentcontext;  Private Onchildclicklistener stvclickevent; //  External callback function          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 ;}  /** * The second layer in the layer-3 tree structure is an expandablelistview.  */          Public View getchildview ( Int Groupposition, Int  Childposition,  Boolean  Islastchild, view convertview, viewgroup parent ){  //  Yes          Final Expandablelistview Treeview = Getexpandablelistview ();  Final Treeviewadapter = New Treeviewadapter ( This . Parentcontext, 0 ); List <Treenode> TMP = treeviewadapter. gettreenode (); //  The temporary variable obtains the treenode set of the treeviewadapter. It can be empty.          Final Treenode = (Treenode) getchild (groupposition, childposition); TMP. Add (treenode); treeviewadapter. updatetreenode (TMP); Treeview. setadapter (treeviewadapter );  //  Key Point: Obtain the Parent and Child Nodes of the selected secondary tree menu and return the result to the external callback function. Treeview. setonchildclicklistener ( This  . Stvclickevent );  /**  * Key point: when the level-2 menu is expanded, the size of the level-3 menu is set by obtaining the number of nodes.  */  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 );}});  /**  * When the level-2 menu is recycled, it is set to the standard item size.  */  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 ;}  /**  * The first layer in the Three-Level tree structure is textview, which is used as the title  */     Public View getgroupview ( Int Groupposition, Boolean  Isexpanded, view convertview, viewgroup parent) {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  ;}} 

Expandablelistviewactivity. Java

List <supertreeviewadapter. supertreenode> supernodetree = Superadapter. gettreenode ();  For ( Int I = 0; I <parent. length; I ++ ) {Supertreeviewadapter. supertreenode Supernode =New  Supertreeviewadapter. supertreenode (); Supernode. Parent = Parent [I];  For ( Int J = 0; j <child_grandchild.length; j ++ ) {Treeviewadapter. treenode Node = New  Treeviewadapter. treenode (); node. Parent = Child_grandchild [J] [0] [0 ];  For ( Int 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 ); 

Main. xml

< 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 = "L2 structure"/> < Button Android: ID = "@ + ID/button2" Android: layout_width = "100dip" Android: layout_height = "Wrap_content" Android: Text = "Three-tier structure"/> </linearlayout> < Expandablelistview Android: ID = "@ + ID/expandablelistview" Android: layout_width = "Fill_parent" Android: layout_height = "Fill_parent"> </expandablelistview>

:

L3 list

 

Layer 2 list

Download source code: expandablelistview.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.