Android Data Adapter-expandablelistview

Source: Internet
Author: User

Previously written about the usage of the ListView in Android, which can be implemented using Arrayadapter,simpleadapter,baseadapter, this time the main use of Expandablelistview shows a two-layer effect, Expandablelistview is an Android can implement the dropdown list of a control similar to the QQ kind of my friend is a row of their friends, is the two-layer effect, the implementation of the words using Simpleexpandablelistadapter can be.

Layout File

Let's look at the effect:

The XML code in main:

  <button        android:onclick= "test"        android:layout_width= "fill_parent"        android:layout_height= "Wrap_ Content "        android:text=" flyelephant "/>    <expandablelistview        android:id=" @id/android:list "        Android:layout_width= "Fill_parent"        android:layout_height= "fill_parent"        android:drawselectorontop= " False "/>

define the Province.xml of a province:

<?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:o rientation= "vertical" >    <textview        android:id= "@+id/list_provincetext"        android:layout_width= " Fill_parent "        android:layout_height=" fill_parent "        android:paddingbottom=" 8px "        android:paddingleft=" 30px "        android:paddingright=" 5px "        android:paddingtop=" 8px "        android:textsize=" 20SP "/></ Linearlayout>

Defines the child.xml of a region:

<?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:o rientation= "vertical" >           <textview        android:id= "@+id/child_text" android:layout_width= "Fill_        Parent "        android:layout_height=" fill_parent "        android:paddingbottom=" 8px "        android:paddingleft=" 30px "        android:paddingright= "5px"        android:paddingtop= "8px"        android:textsize= "20sp"/>    </ Linearlayout>
Demo Implementation

The main implementation of code, the code has been commented, it is not necessary to elaborate, if in doubt can comment on the district discussion:

       Create a level entry list<map<string, string>> provinces = new arraylist<map<string, string>> ()        ;        Create two provincial level entries map<string, string> firstprovince= new hashmap<string, string> ();        Firstprovince.put ("Province", "Henan");        Map<string, string> secondprovince= new hashmap<string, string> ();        Secondprovince.put ("Province", "Beijing");        Provinces.add (firstprovince);        Provinces.add (secondprovince); Create a level two entry under entry list<map<string, string>> childlist1= new arraylist<map<string, string>>        ();        Also in the first level entry directory to create two corresponding level two entries directory map<string, string> child1= new hashmap<string, string> ();        Child1.put ("Child", "Zhengzhou");        map<string, string> child2 = new hashmap<string, string> ();        Child2.put ("Child", "Kaifeng");        Childlist1.add (child1);        Childlist1.add (CHILD2); Ibid. list<map<string, string>> ChildList2 = new arraylist<map<string, string>> ();        map<string, string> child3 = new hashmap<string, string> ();        Child3.put ("Child", "Haidian");        map<string, string> child4 = new hashmap<string, string> ();        Child4.put ("Child", "changping");        Childlist2.add (CHILD3);        Childlist2.add (CHILD4); Place level two entries in a collection for display with list<list<map<string, string>>> childs = new Arraylist<list<map<s        Tring, string>>> ();        Childs.add (CHILDLIST1);        Childs.add (CHILDLIST2);         /** * Use Simpleexpandablelistadapter to display Expandablelistview * Parameter 1. Contextual Object Context * Parameter 2. First Level Entry directory collection         * Parameter 3. The layout file corresponding to the first level entry * parameter 4.fromto, which is the key in the map, specifies the object to display * parameter 5. Corresponding to parameter 4, specify the ID to display in groups * parameter 6. Two-level entry directory Collection * Parameter 7. The layout file corresponding to the two level entry * parameter 8.fromto, which is the key in the map, specifies the object to display * parameter 9. Corresponding to parameter 8, specify the ID to display in the childs */S Impleexpandablelistadapter adapter = new SIMPLEEXPANDABLELISTADAPTEr (this, provinces, R.layout.list_group, new string[] {"Province"}, new int[] {r.id.list        _grouptext}, Childs, R.layout.child, new string[] {"Child"}, new int[] {r.id.child_text}); Setlistadapter (adapter);

forget to say, mainactivity need to inherit expandablelistactivity, let's look at the final effect:

Android Data Adapter-expandablelistview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.