The preface is now back to work, the first thing is Expandlistview optimization. Here is a simple demo to introduce the use of Expandablelistview.
Introduction to the function of demo implementation, mainly inherit baseexpandablelistadapter to define adapter rendering Expandablelistview data:
- Each child item has a TextView and a imageview delete identity.
- When you click on a child item, a toast prompt pops up.
- Child item can be deleted by clicking the delete icon.
- Each time a specific group is expanded. The other group contracted on its own initiative.
Demo Effect Show:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd3p5xze5odg=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Android implementation to create a new Android project, I named it expandtutorial.
The XML layout file Expandtutorial project requires a total of three XML layout files. Each is activity_main.xml. Child_item.xml. Group_item.xml.
Activity_main.xml
This is for the layout of the Expandablelistview, the content such as the following:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/container " android:layout_width=" Match_parent " Android : layout_height= "Match_parent" > <expandablelistview android:id= "@+id/expandable_list_view_id" android:layout_width= "match_parent" android:layout_height= "match_parent" > </ Expandablelistview></relativelayout>
Group_item.xml
This is the style used to define the parent list, consisting of a textview and a ImageView
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" 50DP "android:background=" #ffffff "Android : orientation= "Horizontal" android:gravity= "center_vertical" > <textview android:id= "@+id/group_text "Android:layout_width=" wrap_content "android:layout_height=" Match_parent "android:layout_marginleft= "16DP" android:textcolor= "#808080" android:textsize= "16sp" android:gravity= "center_vertical" a Ndroid:layout_alignparentleft= "true" android:text= "@string/app_name"/> <imageview android:id= " @+id/group_indicator "android:src=" @drawable/ic_guide_listview_down "android:contentdescription=" @null " Android:layout_width= "Wrap_content" android:layout_height= "match_parent" android:layout_marginright= "16sp "Android:layout_alignpareNtright= "true"/> </RelativeLayout>
Child_item.xml This is the style used to define the child list
<?XML version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/child_item_id " android:layout_width=" match_parent " android:layout_height = "Match_parent" android:padding= "10DP" > <textview android:id= "@+id/child_item" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignparentleft= "true" android:layout_centervertical= "true" android:paddingleft= "25DP"/> <imageview android : id= "@+id/child_delete" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" android:contentdescription= "@null" android:src= "@ Drawable/delete "/></relativelayout>
Own definition Adapter own definition adapter is inherited from Baseexpandablelistadapter. There are several areas to note:
- Using a static inner class to identify the weidget of the parent item and the child item, because the IDs of the controls are the same, this saves time for each findviewbyid.
- The override (override) Ischildselectable method returns True to indicate that the child item is clickable.
Package Com.example.expandtutorial;import Java.util.arraylist;import Android.app.alertdialog;import Android.content.context;import Android.content.dialoginterface;import Android.graphics.typeface;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.baseexpandablelistadapter;import Android.widget.ImageView;import Android.widget.textview;public class Customeradapter extends Baseexpandablelistadapter{private ArrayList< Parentobj> datas;private Context Context;public customeradapter (arraylist<parentobj> datas, Context context) {super (); This.datas = Datas;this.context = Context;} @Overridepublic int GetGroupCount () {return datas.size ();} @Overridepublic int getchildrencount (int groupposition) {return datas.get (groupposition). Getchilds (). Size ();} @Overridepublic Object getgroup (int groupposition) {return datas.get (groupposition);} @Overridepublic Object getchild (int groupposition, int cHildposition) {return datas.get (groupposition). Getchilds (). get (childposition);} @Overridepublic long getgroupid (int groupposition) {return groupposition;} @Overridepublic long Getchildid (int groupposition, int childposition) {return childposition;} @Override//Whether there is a stable ID. In relation to the refresh order, the public boolean hasstableids () {return false;} @Overridepublic view Getgroupview (int groupposition, Boolean isexpanded, View Convertview, ViewGroup parent) { Parentviewholder pviewholder;if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate ( R.layout.group_item, parent, false);p Viewholder = new Parentviewholder ();p Viewholder.ptextview = (TextView) Convertview.findviewbyid (r.id.group_text);p Viewholder.pimageview = (ImageView) Convertview.findviewbyid ( R.id.group_indicator); Convertview.settag (Pviewholder);} Pviewholder = (Parentviewholder) convertview.gettag ();p viewHolder.pTextView.setTypeface (null, typeface.bold); PviewHolder.pTextView.setText (Datas.get (groupposition) getpname ()); if (isexpanded) {PVIEWHOlder.pImageView.setImageResource (R.DRAWABLE.IC_GUIDE_LISTVIEW_UP);} else {pviewHolder.pImageView.setImageResource (r.drawable.ic_guide_listview_down);} return Convertview;} @Overridepublic view Getchildview (int groupposition, int childposition, Boolean islastchild, view Convertview,viewgroup Parent) {Childviewholder cviewholder;if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate ( R.layout.child_item, parent, false); Cviewholder = new Childviewholder (); Cviewholder.ctextview = (TextView) Convertview.findviewbyid (r.id.child_item); Cviewholder.cimageview = (ImageView) Convertview.findviewbyid ( R.id.child_delete); Convertview.settag (Cviewholder);} Cviewholder = (Childviewholder) convertview.gettag (); CViewHolder.cTextView.setText (Datas.get (groupposition). Getchilds (). Get (Childposition). Getcname ()); final int gposition = groupposition;final int cPosition = childposition; CViewHolder.cImageView.setOnClickListener (New Onclicklistener () {@Overridepublic void OnClick (View v) {AlertdiaLog. Builder builder = new Alertdialog.builder (context); Builder.setmessage ("Do you want to remove?"); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {datas.get (gposition). Getchilds (). Remove (cPosition); notifydatasetchanged ();}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {dialog.cancel ();}}); Alertdialog Alertdialog = Builder.create (); Alertdialog.show ();}); return Convertview;} @Overridepublic boolean ischildselectable (int groupposition, int childposition) {return true;} Static class Parentviewholder {TextView Ptextview;imageview pimageview;} Static class Childviewholder {TextView Ctextview;imageview cimageview;}}
The activity of our main activity, used to present the UI effect, there are two main places to note:
- Expandablelistview needs to be bound with its own definition of adapter, through the Setadapter method.
- The default Expandablelistview with an indicator arrow. We don't need this indicator to define the layout ourselves. You can use Expandablelistview.setgroupindicator (null) to remove it.
- Set Setongroupexpandlistener to close other group when a specific group is expanded.
- Set Setonchildclicklistener and listen for child click events.
Package Com.example.expandtutorial;import Java.util.arraylist;import Android.app.activity;import android.os.Bundle ; Import Android.view.view;import Android.widget.expandablelistview;import Android.widget.expandablelistview.onchildclicklistener;import Android.widget.expandablelistview.ongroupexpandlistener;import Android.widget.toast;public class MainActivity Extends Activity {private Expandablelistview expandablelistview;private customeradapter customeradapter;private arraylist<parentobj> listData = new arraylist<parentobj> (), @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); CreateGroupData ( ); Createchilddata (); Expandablelistview = (Expandablelistview) Findviewbyid (r.id.expandable_list_view_id); Customeradapter = new Customeradapter (ListData, this); Expandablelistview.setadapter (Customeradapter); Expandablelistview.setgroupindicator (null); Expandablelistview.setongroupexpandlistener (New OnGroupExPandlistener () {@Overridepublic void ongroupexpand (int groupposition) {for (int i = 0; i < Customeradapter.getgroupcoun T (); i + +) {if (i = groupposition) {expandablelistview.collapsegroup (i);}}}); Expandablelistview.setonchildclicklistener (New Onchildclicklistener () {@Overridepublic Boolean Onchildclick ( Expandablelistview Parent, View v, int groupposition, int childposition, long id) {Toast.maketext (mainactivity.this, List Data.get (groupposition). Getchilds (). Get (Childposition). Getcname (), Toast.length_long). Show (); return true;});} private void Creategroupdata () {parentobj P1 = new Parentobj ();p 1.setpName ("HP"); Listdata.add (p1); parentobj P2 = new Parentobj ();p 2.setpName ("DELL"); Listdata.add (p2); Parentobj p3 = new Parentobj ();p 3.setpName ("Lenovo"); Listdata.add (p3); Parentobj P4 = new Parentobj ();p 4.setpName ("Sony"); Listdata.add (p4); Parentobj P5 = new Parentobj ();p 5.setpName ("HCL"); Listdata.add (p5); Parentobj P6 = new Parentobj ();p 6.setpName ("Samsung"); Listdata.add (P6);} private void CreaTechilddata () {//Preparing laptops collection (child) string[] Hpmodels = {"HP Pavilion G6-2014tx", "ProBook hp 4540", "HP Envy 4-1025tx "}; String[] Hclmodels = {"HCl S2101", "HCl L2102", "HCl V2002"}; String[] Lenovomodels = {"IdeaPad Z Series", "Essential G series", "ThinkPad X series", "IdeaPad Z series"}; String[] Sonymodels = {"VAIO E series", "VAIO Z series", "VAIO S series", "VAIO YB series"}; String[] Dellmodels = {"Inspiron", "Vostro", "XPS"}; String[] Samsungmodels = {"NP series", "Series 5", "SF series"};for (Parentobj p:listdata) {if (P.getpname (). Equals ("H P ")) {arraylist<childobj> clists = new arraylist<childobj> (); for (int i = 0; i < hpmodels.length; i + +) {Ch Ildobj cobj = new Childobj () Cobj.setcname (Hpmodels[i]); Clists.add (CObj);} P.setchilds (clists);} else if (P.getpname () equals ("DELL")) {arraylist<childobj> clists = new arraylist<childobj> (); for (int i = 0; i < dellmodels.length; i + +) {childobj cobj = new Childobj (); cobj.setcname(Dellmodels[i]); Clists.add (CObj);} P.setchilds (clists);} else if (P.getpname (). Equals ("Lenovo")) {arraylist<childobj> clists = new arraylist<childobj> (); for (int i = 0; i < lenovomodels.length; i + +) {childobj cobj = new Childobj (); Cobj.setcname (Lenovomodels[i]); Clists.add (CObj);} P.setchilds (clists);} else if (P.getpname () equals ("Sony")) {arraylist<childobj> clists = new arraylist<childobj> (); for (int i = 0; i < sonymodels.length; i + +) {childobj cobj = new Childobj (); Cobj.setcname (Sonymodels[i]); Clists.add (CObj);} P.setchilds (clists);} else if (P.getpname () equals ("HCL")) {arraylist<childobj> clists = new arraylist<childobj> (); for (int i = 0; i < Hclmodels.length; i + +) {childobj cobj = new Childobj (); Cobj.setcname (Hclmodels[i]); Clists.add (CObj);} P.setchilds (clists);} else {arraylist<childobj> clists = new arraylist<childobj> (); for (int i = 0; i < samsungmodels.length; i + +) {Childobj CObj = new Childobj (); Cobj.setcname (SamsunGmodels[i]); Clists.add (CObj);} P.setchilds (clists);}}} Class Parentobj {private String pname;private arraylist<childobj> childs = new arraylist<childobj> ();p ublic String Getpname () {return pName;} public void Setpname (String pName) {this.pname = PName;} Public arraylist<childobj> Getchilds () {return childs;} public void Setchilds (arraylist<childobj> childs) {this.childs.clear (); This.childs.addAll (childs);}} Class Childobj {private string Cname;public string Getcname () {return cName;} public void Setcname (String cName) {this.cname = CName;}}
Source code I will upload the source code to CSDN, you can download for free, welcome thread discussion. Source code Address: http://download.csdn.net/detail/zinss26914/7532205
Android:expandablelistview use