Use of expandablelistview

Source: Internet
Author: User

Expandablelistview is a drop-down list control that can be implemented in Android. The specific implementation method is as follows:

First, define an expandablelistview in the XML file of layout.

<Linearlayout <br/> Android: Id = "@ + ID/linearlayout" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> androidrientation = "vertical"> <br/> <expandablelistview <br/> Android: id = "@ + ID/expandablelistview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content"/> <br/> </linearlayout>

 

Define two lists to store strings in group/child controls.

Private list <string> grouparray; <br/> private list <string> childarray;

 

Initialize the two lists and insert some data.

Grouparray = new arraylist <string> (); <br/> childarray = new arraylist <list <string> (); <br/> grouparray. add ("first line"); <br/> grouparray. add ("second line"); </P> <p> List <string> temparray = new arraylist <string> (); <br/> temparray. add ("first"); <br/> temparray. add ("second"); <br/> temparray. add ("Article 3"); </P> <p> for (INT Index = 0; index <grouparray. size (); ++ index) {<br/> childarray. add (temparray); <br/>}

 

Define the adapter of expandablelistview

// Expandablelistview adapter <br/> public class expandableadapter extends baseexpandablelistadapter <br/>{< br/> activity; </P> <p> Public expandableadapter (Activity) <br/>{< br/> activity = A; <br/>}< br/> Public object getchild (INT groupposition, int childposition) <br/>{< br/> return childarray. get (groupposition ). get (childposition); <br/>}< br/> Public long getchildid (INT groupposition, int childposition) <br/>{< br/> return childposition; <br/>}< br/> Public int getchildrencount (INT groupposition) <br/>{< br/> return childarray. get (groupposition ). size (); <br/>}< br/> Public View getchildview (INT groupposition, int childposition, <br/> Boolean islastchild, view convertview, viewgroup parent) <br/>{< br/> string = childarray. get (groupposition ). get (childposition); <br/> return getgenericview (string); <br/>}< br/> // group method stub <br/> Public object getgroup (INT groupposition) <br/>{< br/> return grouparray. get (groupposition); <br/>}< br/> Public int getgroupcount () <br/>{< br/> return grouparray. size (); <br/>}< br/> Public long getgroupid (INT groupposition) <br/>{< br/> return groupposition; <br/>}< br/> Public View getgroupview (INT groupposition, Boolean isexpanded, <br/> View convertview, viewgroup parent) <br/>{< br/> string = grouparray. get (groupposition); <br/> return getgenericview (string ); <br/>}< br/> // view stub to create group/Children's view <br/> Public textview getgenericview (string) <br/>{< br/> // layout parameters for the expandablelistview <br/> abslistview. layoutparams = new abslistview. layoutparams (<br/> viewgroup. layoutparams. fill_parent, 64); <br/> textview text = new textview (activity); <br/> text. setlayoutparams (layoutparams); <br/> // center the text vertically <br/> text. setgravity (gravity. center_vertical | gravity. left); <br/> // set the text starting position <br/> text. setpadding (36, 0, 0, 0); <br/> text. settext (string); <br/> return text; <br/>}< br/> Public Boolean hasstableids () <br/>{< br/> return false; <br/>}< br/> Public Boolean ischildselectable (INT groupposition, int childposition) <br/>{< br/> return true; <br/>}< br/>}

 

Add an adapter to the last defined expandablelistview.

Expandablelistview = (expandablelistview) findviewbyid (R. Id. expandablelistview); <br/> expandablelistview. setadapter (New expandableadapter (main. This)

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.