Recently in the enterprise unforgettable record, have to use Expandablelistview, now has been done.
This requires two layouts, as follows
The first tt_item_group_parent.xml, if you want to set the height of the parent or child components, it is best to set in the two interfaces, the other settings are not how to change, if someone solve, say to share it
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:gravity= "Center_vertical" android:orientation= "horizontal" android:background= "#ffffff" android:padding= " 10DP "> <textview android:id=" @+id/tt_item_group_parent_name " android:layout_width=" Wrap_ Content " android:layout_height=" wrap_content " android:layout_marginleft=" 30dip " android: Textcolor= "@color/black" android:textsize= "17sp"/> <textview android:id= "@+id/tt_item_ group_parent_id " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " Android:visibility= "Gone"/></linearlayout>
A second Tt_item_group_child.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" Horizontal "> <com.chat.ui.widget.imageviewcircle android:id=" @+id/tt_item_group_child_icon "Android:layout_wi Dth= "45DP" android:layout_height= "45DP" android:layout_marginright= "10DP" android:scaletype= "CENTERCR Op "/> <textview android:id=" @+id/tt_item_group_child_name "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_marginleft= "10DP" android:textcolor= "@color/black" Android:textsize= "20sp"/> <textview android:id= "@+id/tt_item_group_child_id" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:visibility= "Gone"/></LINEARLAYOUT&G T
Now it's time to write the container. Groupadapter.java
Package Com.chat.ui.adapter;import Java.util.list;import Com.chat.service.aidl.contact;import Com.chat.service.aidl.groupinfo;import Com.chat.ui.widget.imageviewcircle;import Com.chat.IM;import Com.chat.R; Import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseexpandablelistadapter;import Android.widget.TextView;public Class Groupadapter extends Baseexpandablelistadapter{private list<groupinfo> grouplist;private List<Contact > contactlist;private layoutinflater mlayoutinflater = Null;public groupadapter (Context ctx,list<groupinfo> grouplist) {this.grouplist = Grouplist;mlayoutinflater = (layoutinflater) ctx.getsystemservice (Context.LAYOUT_ Inflater_service);} Gets the list of specified groups, sub-list data at the specified sub-list @overridepublic Object getchild (int grouppos, int childpos) {contactlist = Grouplist.get ( Grouppos). Getcontactlist (); return Contactlist.get (Childpos);} @Overridepublic long Getchildid (int grounppos, int chilDPos) {return childpos;} Returns the number of sub-lists at a specific group @overridepublic int getchildrencount (int grouppos) {contactlist = Grouplist.get (Grouppos). Getcontactlist (); return contactlist.size ();} This method determines the appearance of each sub-option @overridepublic view Getchildview (int grouppos, int childpos, Boolean islastchild, view Convertview, ViewGroup parent) {Childholder holder = null;if (Convertview = = null) {Convertview = Mlayoutinflater.inflate (r.layout.tt_ Item_group_child, null); holder = new Childholder (Convertview); Convertview.settag (holder);} Else{holder = (Childholder) Convertview.gettag ();} Contact Contact = (Contact) getchild (grouppos,childpos); Holder.name.setText (Contact.name); Holder.icon.setImageDrawable (Im.getavatar (Contact.account)); return Convertview;} Gets the data at the specified group location @overridepublic groupinfo getgroup (int grouppos) {return grouplist.get (Grouppos);} @Overridepublic int GetGroupCount () {return grouplist.size ();} @Overridepublic long getgroupid (int grouppos) {return grouppos;} @Overridepublic View getgroupview (int grouppos, Boolean isexpanded, View Convertview, ViewGroup parent) {Groupholder holder = null;if (Convertview = = null) {Convertview = Mlayoutinflater . Inflate (r.layout.tt_item_group_parent, null); holder = new Groupholder (Convertview); Convertview.settag (holder);} Else{holder = (Groupholder) Convertview.gettag ();} GroupInfo info = (groupinfo) getgroup (Grouppos); System.out.println ("Groupadapter->getgroupview:" +info.getname ()); Holder.name.setText (Grouplist.get (grouppos ). GetName ()); return Convertview;} Whether to specify a background data change for the ID of the grouped view and its child views also preserves that ID. @Overridepublic Boolean hasstableids () {return true;} Specifies whether the child view of the location can be selected. @Overridepublic boolean ischildselectable (int grouppos, int childpos) {return true;} Class childholder{imageviewcircle icon; TextView name; TextView accout;public Childholder (View v) {icon = (imageviewcircle) V.findviewbyid (R.id.tt_item_group_child_icon); Name = (TextView) V.findviewbyid (r.id.tt_item_group_child_name); accout = (TextView) V.findviewbyid (r.id.tt_item_ group_child_id);}} class Groupholder{textview name; TextView Accout;public Groupholder (View v) {name = (TextView) V.findviewbyid (r.id.tt_item_group_parent_name); accout = (TextView ) V.findviewbyid (r.id.tt_item_group_parent_id);}}}
The container is also written, of course the data is loaded with activity
public class Addfriactivity extends Activity {private Expandablelistview list;private groupadapter adapter;public void on Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.tt_group); list = ( Expandablelistview) Curview.findviewbyid (r.id.tt_fragment_group_list); init ();} private void InitData () {list<groupinfo> grouplist = new arraylist<groupinfo> (); List<contact> contactlist = null; GroupInfo groupinfo = null; Contact Contact = Null;contactlist = new arraylist<contact> (), contact = new Contact (); contact.account = "Xiaoming"; contac T.name = "Xiaoming"; Contactlist.add (contact), contact = new Contact (), Contact.account = "Little Red"; contact.name = "Little Red"; Contactlist.add (contact); groupinfo = new GroupInfo (); Groupinfo.setname ("development"); Groupinfo.setcontactlist ( ContactList); Grouplist.add (groupinfo);//**********************contactlist = new arraylist<contact> (); Contact = new Contact (); contact.account = "Daming"; contact.name = "Daming"; contactlist.add (contact); contact = new Contact (); contact.account = "Scarlet"; contact.name = "Scarlet"; contactlist.add (contact); groupinfo = new GroupInfo (); Groupinfo.setname ("sales Department"); Groupinfo.setcontactlist (contactlist); Grouplist.add (groupinfo);//******************** **contactlist = new arraylist<contact> (); contact = new Contact (); contact.account = "Zhong ming"; contact.name = "Zhong Ming"; Contactlist.add (contact); groupinfo = new GroupInfo (); Groupinfo.setname ("Finance Department"); Groupinfo.setcontactlist ( ContactList); Grouplist.add (GroupInfo);//**********************for (int i=0;i<grouplist.size (); i++) { System.out.println (Grouplist.get (i). GetName ()); for (int j=0;j<grouplist.get (i). Getcontactlist (). Size (); j + +) { System.out.println ("" +grouplist.get (i). Getcontactlist (). Get (j). name);}} adapter = new Groupadapter (getactivity (), grouplist); List.setadapter (adapter);}}
Remember there are two auxiliary classes to write about
Class GroupInfo {private String name;private list<contact> contactlist;public list<contact> getContactList () {return contactlist;} public void Setcontactlist (list<contact> contactlist) {this.contactlist = contactlist;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}} Class Contact{public string account, Name;public contact () {}public string Getaccount () {return account;} public void Setaccount (String account) {this.account = account;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Finally write the layout of the activity inside
<!-- android:cachecolorhint= "#00000000", this setting removes the effect of turning the background black when dragging view android:listselector= "#00000000", You can remove the yellow background when selected-- <expandablelistview android:id= "@+id/tt_fragment_group_list" android:layout_ Width= "Fill_parent" android:layout_height= "wrap_content" android:padding= "5dip" android: Cachecolorhint= "@android: color/transparent" android:background= "#ffffff" android:listselector= "@ Android:color/transparent " android:groupindicator=" @drawable/tt_drop_select " android:divider=" @ Drawable/tt_divide_line " android:childdivider=" @drawable/tt_divide_line " />
Almost forgot the effect of the click, see below
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/tt_drop_up " android:state_expanded=" false " android:drawingcachequality= "Auto"/> <item android:drawable= "@drawable/tt_drop_down" Android : State_expanded= "true" android:drawingcachequality= "Auto"/></selector>
icon in the following, want to use the words directly download it, the icon is a bit asymmetric, I will not P-map, casually under the
Even the icons are uploaded.
Finally write well, how to feel like Wang aunt's smelly feet, and smelly and long, may be I do in the process of a lot of details are encountered difficulties, so just write out, I hope others do not encounter the time
Now look at the effect.
Android's Expandablelistview