Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
Expandablelistview is a vertical scrolling view of level two list items, and unlike a ListView, it can have two layers: each layer can be expanded independently and display its children.
Friends QQ list, you can expand, can be put up, in Android, used to be more than the ListView, although the list can be implemented, but in some cases, we still want to use the list can be grouped and to achieve contraction, That will use the Android Expandablelistview, today studied the use of this, but also refer to a lot of data to write a small demo, to achieve the basic functions, the following direct and source code ~!
The effect of this article:
Effect one source download (resources are swallowed, come again tomorrow)
Effect two source download (resources are swallowed, come again tomorrow)
Directory:
First, the realization principle
Second, layout and code
Three, custom icons
Four, the icon placed to the right
First, the realization principle
1, you must first define a Expandablelistview in the layout file
2, next create the layout file corresponding to the first level entry group
3. Create a two-level entry corresponding to the layout file child
4, the activity that loads the Expandablelistview component must inherit from expandablelistactivity
Second, layout and code
1, first in the main layout activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical "> < Expandablelistview android:id= "@id/android:list" android:layout_width= "Fill_parent" android: layout_height= "Fill_parent"/></linearlayout>
2. Next, define the layout-level list in the Drawable folder Groups.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= " Vertical " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <textview android:id= "@+id/textgroup" android:layout_width= "fill_parent" android:layout_height= "fill_parent " android:paddingleft=" 40px " android:paddingtop=" 6px " android:paddingbottom=" 6px " android: Textsize= "25SP" android:text= "No data" /></linearlayout>
3. Then define the layout level two list in the Drawable folder Childs.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= " Vertical " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <textview android:id= "@+id/textchild" android:layout_width= "fill_parent" android:layout_height= "fill_parent " android:paddingleft=" 60px " android:paddingtop=" 10px " android:paddingbottom=" 10px " android: Textsize= "20SP" android:text= "No Data"/></linearlayout>
4, then initialize and use the
Package Com.example.expandablelistview;import Java.util.arraylist;import Java.util.hashmap;import java.util.List; Import Java.util.map;import Android.os.bundle;import Android.app.expandablelistactivity;import Android.util.displaymetrics;import Android.view.view;import Android.widget.expandablelistview;import Android.widget.simpleexpandablelistadapter;import Android.widget.toast;public class MainActivity extends expandablelistactivity {/** * Creates a level entry container */list<map<string, string>> gruops = new arraylist<map<string, String>> ();/** * Store content so that it appears in the list */list<list<map<string, string>>> childs = new arraylist< List<map<string, string>>> (); @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Setlistdata ();} /** * Set list contents */public void Setlistdata () {//Create two level entry titles map<string, string> title_1 = new hashmap<string, STRING&G t; (); Map<string, string> Title_2 = new hashmap<string, string> (); map<string, string> title_3 = new hashmap<string, string> (); Title_1.put ("group", "Lin Bingwen"); Title_2.put (" Group "," Wenbinglin "); Gruops.add (title_1); Gruops.add (title_2);//create level Two entry content//content one map<string, string> title_1_content_1 = new hashmap<string, string> (); map<string, string> title_1_content_2 = new hashmap<string, string> (); map<string, string> title_1_content_3 = new hashmap<string, string> (); Title_1_content_1.put ("Child", "worker" ); Title_1_content_2.put ("Child", "student"), Title_1_content_3.put ("Child", "Farmer"); list<map<string, string>> childs_1 = new arraylist<map<string, string>> (); Childs_1.add (title _1_content_1); Childs_1.add (title_1_content_2); Childs_1.add (title_1_content_3);//Content two map<string, String> title_2_content_1 = new hashmap<string, string> (); map<string, string> title_2_content_2 = new hashmap<string, string> (); map<string, string> title_2_content_3 = new HashMap<string, String> () title_2_content_1.put ("Child", "orangutan"), Title_2_content_2.put ("Child", "Tiger"); title_2_ Content_3.put ("Child", "lion"); list<map<string, string>> childs_2 = new arraylist<map<string, string>> (); Childs_2.add (title _2_content_1); Childs_2.add (title_2_content_2); Childs_2.add (title_2_content_3); Childs.add (childs_1); Childs.add ( childs_2);/** * Create expandablelist Adapter container parameters: 1. Context 2. First-level collection 3. First-level style file 4. First level Entry key value * 5. First level Display control name 6. Second level set 7. Secondary style 8. Level Two entry key value 9. Two level Display control name * */simpleexpandablelistadapter sela = new Simpleexpandablelistadapter (this, Gruops, r.drawable. Groups, new string[] {"group"},new int[] {r.id.textgroup}, Childs, r.drawable.childs,new string[] {"Child"}, new int [] {r.id.textchild});//Join List setlistadapter (SELA);} /** * List Contents Press */@Overridepublic Boolean Onchildclick (expandablelistview parent, View v,int groupposition, int childpositio n, long id) {Toast.maketext (Mainactivity.this, "You have selected" + Gruops.get (groupposition). toString () + "sub-number" + Childs.get ( Groupposition). Get (Childposition). toString (), Toast.length_short). Show (); return Super.onchildclick (parent, V, Groupposition, childposition, id);} /** * Level Two title Press */@Overridepublic boolean setselectedchild (int groupposition, int childposition,boolean shouldexpandgroup) {return Super.setselectedchild (groupposition, childposition,shouldexpandgroup);} /** * First Title Press */@Overridepublic void setselectedgroup (int groupposition) {super.setselectedgroup (groupposition);}}
5. Effect
This is the effect on my phone, click Workers. Students and so on the level two list, my phone will have a prompt box appears, but I do not know why the record is not.
SOURCE Free Download
Third, custom list icon
The above icon is generated by the system itself, below we want to change to their own
1. Change the custom icon
Create a new Expandablelistview_change.xml under the Drawable folder
<?xml Version = "1.0" encoding = "Utf-8"? ><selector xmlns:android = "http://schemas.android.com/apk/ Res/android "> <item android:state_expanded =" true " android:drawable =" @drawable/w2 "/> < Item android:drawable = "@drawable/w1"/>
2, modify the above layout Activity.main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical "> < Expandablelistview android:id= "@id/android:list" android:layout_width= "fill_parent" android:layout _height= "Fill_parent" android:background= "#f5f5f5" android:cachecolorhint= "#f5f5f5" android: Groupindicator= "@drawable/expandablelistview_change"/></linearlayout>
In fact, I added a sentence.
Android:groupindicator= "@drawable/expandablelistview_change"
Let's take a look at the results:
SOURCE Free Download
Four, the icon placed to the right
In the above Mainactivity.java function Setlistdata () plus:
Get the size of the screen displaymetrics DM = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);// The icon is set to the right Getexpandablelistview (). Setindicatorbounds (dm.widthpixels-60, dm.widthpixels); Set the location of the indicator icon
Effect:
SOURCE Free Download:
Android list shrink and expand faux QQ Friends List (very detailed, attached source)