List of Android Expandablelistview list

Source: Internet
Author: User

Sometimes, we need to design such an interface, there is a list outside, when we click on one of the items in the list, we will expand the entry, a new list appears. For example: (program running, posted here)

When we click on the first item, the view changes to:

--------------------------------------------------------------------------------------------------------------- ---------------

To achieve this, you need to define three layouts, including the displayed main_activity (main layout), group (first-level list layout), child (second-level list layout), the following code, and explanations in the comments:

Main_activity:

<LinearLayoutxmlns: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:orientation= "vertical"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= "Com.example.expandlistaacitvity.MainActivity" >    <!--Android documents require Expandablelistview ID must be list, otherwise it will throw an error, interested can try #ff0000 for Red Drawselectorontop=false is set when you choose You can clearly display the current item (I tried it as if nothing had changed ...). ) -    <ExpandablelistviewAndroid:id= "@android: Id/list"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:drawselectorontop= "false"/>        <!--This textview is displayed when there is no data to display. This ID also stipulates that must use empty, no person in front of the words will not function -    <TextViewAndroid:id= "@android: Id/empty"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#ff0000"Android:text= "No data" /></LinearLayout>

Group.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >        <!--This is the layout of the first-level list defined #0000ff是深青色 -    <TextViewAndroid:id= "@+id/groupto"Android:layout_height= "Match_parent"Android:layout_width= "Match_parent"Android:background= "#0000ff"Android:paddingtop= "20DP"Android:paddingleft= "60DP"Android:paddingbottom= "10DP"android:textsize= "26SP"Android:text= "No data"/></LinearLayout>

Child.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_margin= "5DP"android:orientation= "vertical" >        <!--The second-level list in the list is defined here, and no data is displayed -    <TextViewAndroid:id= "@+id/childto"Android:layout_height= "Match_parent"Android:layout_width= "Match_parent"Android:paddingtop= "20DP"Android:paddingleft= "50DP"Android:paddingbottom= "10DP"Android:background= "#00ff00"Android:text= "No data"/></LinearLayout>

The following is the main activity, which is also explained in the code:

 Packagecom.example.expandlistaacitvity;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.app.ExpandableListActivity;ImportAndroid.os.Bundle;ImportAndroid.widget.SimpleExpandableListAdapter; Public classMainactivityextendsexpandablelistactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); //define two first-level entrieslist<map<string, string>> groups =NewArraylist<map<string,string>>(); Map<string, string> group1 =NewHashmap<string, string>(); Group1.put ("Group", "Group1 first level list One");        Groups.add (group1); Map<String,String> group2 =NewHashmap<string, string>(); Group2.put ("Group", "Group2 First Level list");                Groups.add (group2); //Define a list that contains level two entries in the first level entrylist<map<string, string>> child1 =NewArraylist<map<string,string>>(); Map<string, string> child1date1 =NewHashmap<string, string>(); Child1date1.put ("Child", "Clild1date1 Second Level list");        Child1.add (CHILD1DATE1); Map<string, string> child1date2 =NewHashmap<string, string>(); Child1date2.put ("Child", "Child1date2 Second Level list");                Child1.add (CHILD1DATE2); //defines a list, which is a level two entry for the second first level entrylist<map<string, string>> child2 =NewArraylist<map<string,string>>(); Map<string, string> child2date1 =NewHashmap<string, string>(); Child2date1.put ("Child", "Child222date1 Second Level list");                Child2.add (CHILD2DATE1); //put two two-level entries into the listlist<list<map<string, string>>> childs =NewArraylist<list<map<string,string>>>();        Childs.add (child1);                Childs.add (CHILD2); /** 1. Context * 2. The data for the first level list, including the individual group * 3. Specifies the layout of a list of levels * 4. Specifies the key of the first-level list * 5. Specifies the ID of the control displayed for a level entry * Following ibid .*/Simpleexpandablelistadapter Simpleexpandablelistadapter=NewSimpleexpandablelistadapter ( This, groups, R.layout.group,Newstring[]{"Group"},New int[]{r.id.groupto}, Childs,r.layout.child,Newstring[]{"Child"},New int[]{r.id.childto}];    Setlistadapter (Simpleexpandablelistadapter); }}

List of Android Expandablelistview list

Related Article

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.