1. Expandablelistview Introduction
The drop-down list (the Extensible list control) is very common in app apps, a control that we have to master in Android development, and here's a look at the development of the Expandablelistview control.
Expandablelistview is divided into group list items and sub-list items, and clicking on the group list item displays all the sub-list items in this group. Like a ListView, it is also the interface between the data and the display through the adapter data adapter, but it uses the other interfaces: Expandablelistadapter.
What we're going to do today is implement a class that inherits its parent interface: Baseexpandablelistadapter, to implement a custom drop-down list (just like in a ListView, to set the layout of the interface layouts for its list items).
2. Example
The sample contains four files, two Java files, and two layout files. The Java file contains an activity, and our own implementation of the Myexpandadapter;layout file is used for activity, and the other is the sub-list item interface layout file, as for the layout of the group list item, We do this by creating a TextView directly in the code, which introduces two ways to implement the drop-down list style.
Plan_manager.xml
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Fill_parent"android:layout_height="Fill_parent" Android:orientation="vertical" > <expandablelistviewandroid:id="@+id/expandablelistview_plan_manager" android:layout_width="Match_parent"android:layout_height="Wrap_content" > </expandablelistview></linearlayout>
Layout files, adding controls
Planmanager.java
PackageCom.plan;ImportJava.util.ArrayList;ImportJava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.ExpandableListView;ImportAndroid.widget.Toast;ImportCOM.EXAMPLE.PLAN.R; Public class planmanager extends Activity{Expandablelistview sp_date_list =NULL;//ListMyexpandadapter adapter =NULL;//Data adapterList<string> Group_head;//Group list item, each group has a child listList<list<string>> child;//Sub-list items @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.plan_manger);//Get controlSp_date_list = (Expandablelistview) Findviewbyid (R.id.expandablelistview_plan_manager);//Initialize group, sub-list itemGroup_head =NewArraylist<string> (); Child =NewArraylist<list<string>> ();//Set adapterSp_date_list.setadapter (NewMyexpandadapter ( This, Group_head,child));//Set data adapterSp_date_list.setcachecolorhint (0);//Drag the list without a black backgroundAddGroup ("Quiet Night Thinking"); AddGroup ("Dawn"); AddChild (0,"The moon Light in front of the bed"); AddChild (0,"The ground frost."); AddChild (1,"Spring sleep does not feel Xiao"); AddChild (1,"Smell the birds everywhere"); }//Add Group list item Public void AddGroup(String Group) {Group_head.add (group); Child.add (NewArraylist<string> ());adding a new array to the//child}//Add a self-list item for the corresponding group Public void AddChild(intPosition,string child) {list<string> it = This. Child.get (position);if(It! =NULL) {It.add (child); }Else{it =NewArraylist<string> (); It.add (child); } }//Sub-list item is selected response method Public void Childselect(intGroupposition,intchildposition) {Toast.maketext (Getbasecontext (), integer.tostring (groupposition) +":"+ integer.tostring (childposition), -). Show (); }}
Find controls, connect to display with adapters, and add some sample data where AddGroup and Addchild and Childselect can be provided to other components as interfaces
Plain_text.xml
<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:layout_width="Fill_parent"android:layout_height="Fill_parent" > <TextViewandroid:id="@+id/textview_plain_text"android:layout_width= "Wrap_content" android:layout_height="Wrap_content"android:text="TextView" /> </linearlayout>
This is a sub-list item layout file, because it is just a simple example, too complicated to learn trouble, so only a text box, in the actual project can add a few controls, change the type of child (preferably map map) is good
Myexpandadapter.java
PackageCom.plan;ImportJava.util.ArrayList;ImportJava.util.List;ImportAndroid.content.Context;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AbsListView;ImportAndroid.widget.BaseExpandableListAdapter;ImportAndroid.widget.TextView;ImportCOM.EXAMPLE.PLAN.R; Public class myexpandadapter extends baseexpandablelistadapter{List<string> Group_head; List<list<string>> child; Planmanager context; Public Myexpandadapter(Context content) {//Initialize group, sub-list itemGroup_head =NewArraylist<string> (); Child =NewArraylist<list<string>> (); } Public Myexpandadapter(Planmanager context,list<string> Group_head, list<list<string>> Child) { This. Context = Context;//Initialize group, sub-list item This. Group_head = Group_head; This. Child = child; }@Override Public int GetGroupCount() {//TODO auto-generated method stub return This. Group_head.size (); }@Override Public int Getchildrencount(intPosition) {//TODO auto-generated method stub if(position<0|| position>= This. Child.size ())return 0;returnChild.get (position). Size (); }@Override PublicObjectGetgroup(intGroupposition) {//TODO auto-generated method stub returnGroup_head.get (groupposition); }@Override PublicObjectGetchild(intGroupposition,intChildposition) {//TODO auto-generated method stub returnChild.get (childposition). get (Childposition); }@Override Public Long Getgroupid(intGroupposition) {//TODO auto-generated method stub returnGroupposition; }@Override Public Long Getchildid(intGroupposition,intChildposition) {//TODO auto-generated method stub returnChildposition; }@Override Public Boolean Hasstableids() {//TODO auto-generated method stub return true; }@Override PublicViewGetgroupview(intGroupposition,Booleanisexpanded, View Convertview, ViewGroup parent) {//Get textString Text = Group_head.get (groupposition);if(Convertview = =NULL){//The Group list interface has only one text box, directly generatedConvertview =NewTextView (context);//Setup interface, Abslistview: base class for virtual lists for implementing entriesAbslistview.layoutparams LP =NewAbslistview.layoutparams (ViewGroup.LayoutParams.FILL_PARENT, -); ((TextView) convertview). Setgravity (gravity.center_vertical | Gravity.left);//text box on the centerConvertview.setpadding ( $,0,0,0);//Set the drop-down icon in the text a little fartherCONVERTVIEW.SETLAYOUTPARAMS (LP); LOG.D ("Group", text); } ((TextView) convertview). SetText (text);returnConvertview; }@Override PublicViewGetchildview(intGroupposition,intChildposition,BooleanIslastchild, View Convertview, ViewGroup parent) {//Sub-list control through interface file design if(Convertview = =NULL){//convert is reused in the run, and if it is not empty, it does not need to be fetched againLayoutinflater Layoutinflater;//Use this to load the interfaceLayoutinflater = Layoutinflater.from (context); Convertview = Layoutinflater.inflate (R.layout.plain_text,NULL); } TextView TV = (TextView) Convertview.findviewbyid (R.id.textview_plain_text); String Text = Child.get (groupposition). get (Childposition); Tv.settext (text);//Get the text control and set the value returnConvertview; }@Override Public Boolean ischildselectable(intGroupposition,intChildposition) {//Call the Childselect method in activityContext.childselect (groupposition,childposition);return true; }}
After the interface is inherited, eclipse can automatically generate these methods, and we just need to change them just fine. There's some place in it. Log.d method, used to output the log in the Logcat (also available System.out.println), mainly used to optimize the program, by looking at the log print several times, you can see whether the method in the program has been repeated calls.
Android Essentials uses Expandablelistview to implement a custom drop-down list