The previous articles introduced the ListView, but in the actual development also often uses the multi-layered listview to display the data, for example QQ in the friend display, therefore this one to understand Expandablelistview, the basic idea and the ListView is roughly the same, So it would be more convenient to use them.
Realize:
Program code:
Layout file:
Activity_main.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <expandablelistview android:id=" @+id/expandablelistview " android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" > </expandablelistview></ Relativelayout>
The layout that group and child use together (of course can also be implemented using different layouts), this layout is relatively simple, with only one textview:
List_item:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <textview android:id=" @+id/textview " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content"/></relativelayout>
Mainactivity:
Package Com.listviewdemo_expandablelistview;import Java.util.arraylist;import Java.util.list;import Android.app.activity;import Android.os.bundle;import Android.widget.expandablelistview;public class MainActivity Extends Activity {private Expandablelistview listview;private list<string> group;private list<list<string >> child;private myadapter adapter; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); ListView = (Expandablelistview) Findviewbyid ( R.id.expandablelistview);/** * Initialize Data */initdata (); adapter = new Myadapter (this,group,child); Listview.setadapter ( adapter);} private void InitData () {group = new arraylist<string> (); child = new arraylist<list<string>> (); Addinfo ("Beijing", New string[]{"Chaoyang", "Haidian", "Dongcheng", "Xicheng"}), Addinfo ("Hebei", New string[]{"Handan", "Shijiazhuang", "Xingtai"}), Addinfo ("Guangdong", New string[]{"Guangzhou", "Shenzhen", "Zhuhai"}); /** * Add data information * @param g * @param c */private void Addinfo (String g,string[] c) {Group.add (g); list<string> list = new arraylist<string> (); for (int i = 0; i < c.length; i++) {List.add (c[i]);} Child.add (list);}}
Myadapter:
Package Com.listviewdemo_expandablelistview;import Java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseexpandablelistadapter;import android.widget.textview;/** * Expandablelistview Adapter * */public class Myadapter extends Baseexpandablelistadapter {private Context context;private list<string> group;private List< List<string>> Child;public Myadapter (context context, list<string> group,list<list<string> > Child) {this.context = Context;this.group = Group;this.child = child;} @Overridepublic int GetGroupCount () {return group.size ();} @Overridepublic int getchildrencount (int groupposition) {return child.size ();} @Overridepublic Object getgroup (int groupposition) {return group.get (groupposition);} @Overridepublic Object getchild (int groupposition, int childposition) {return child.get (childposition). Get ( childposition);} @Overridepublic long getgroupid (int grOupposition) {return groupposition;} @Overridepublic long Getchildid (int groupposition, int childposition) {return childposition;} @Overridepublic Boolean hasstableids () {return false;} /** * Display: Group */@Overridepublic View getgroupview (int groupposition, Boolean isexpanded,view convertview, ViewGroup Pare NT) {Viewholder holder;if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate (R.layout.list_item , null); holder = new Viewholder (); Holder.textview = (TextView) Convertview.findviewbyid (R.id.textview); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.textView.setText (Group.get (groupposition)); Holder.textView.setTextSize (+); Holder.textView.setPadding ( 0); return convertview;} /** * Display: Child */@Overridepublic view Getchildview (int groupposition, int childposition,boolean islastchild, view convert View, ViewGroup parent) {Viewholder holder;if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate ( R.layout.liSt_item, null); holder = new Viewholder (); Holder.textview = (TextView) Convertview.findviewbyid (R.id.textview); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.textView.setText (Child.get (groupposition). Get (Childposition)); Holder.textView.setTextSize (20); Holder.textView.setPadding (0, ten); return Convertview;} Class Viewholder {TextView TextView;} @Overridepublic boolean ischildselectable (int groupposition, int childposition) {return false;}}
Now CSDN Blog to join the link needs to be approved before publishing, so you need source code can be found in the resources I upload.