[Original] Android ExpandableListView, expandablelistview

Source: Internet
Author: User

[Original] Android ExpandableListView, expandablelistview
The use of ExpandableView can be bound to SimpleExpandableListAdapter, mainly to see how to use this Adapter. The default constructor of this class has nine parameters, which explain what is smelly and long.
Public SimpleExpandableListAdapter (Context context, List <? Extends Map <String,?> GroupData, int groupLayout, String [] groupFrom, int [] groupTo, List <? Extends List <? Extends Map <String,? >>> ChildData, int childLayout, String [] childFrom, int [] childTo)

But you don't have to worry about it. You just need to divide the groups starting with Group and starting with Child into two groups. For more information, see the following comments.

Code:
Package com. example. zzp. testexpandablelist; import android. app. activity; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. widget. expandableListView; import android. widget. simpleExpandableListAdapter; import java. util. arrayList; import java. util. hashMap; import java. util. map; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundl E savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ExpandableListView list = (ExpandableListView) findViewById (R. id. expand_list); ArrayList <Map <String, String> groupData = new ArrayList <> (); int groupLayout = R. layout. group_view_group; String [] groupFrom = {"name", "type", "description"}; int [] groupTo = maid _ view_name, R.id.txt _ view_type, R. I D.txt _ view_description}; ArrayList <Map <String, String> childData = new ArrayList <> (); int childLayout = R. layout. group_view_child; String [] childFrom = {"ability"}; int [] childTo = maid _ view_child_ability}; for (int I = 0; I <3; I ++) {// initialize the group data HashMap <String, String> map = new HashMap <> (); map. put ("name", "device" + String. valueOf (I); map. put ("type", "type" + String. value Of (I); map. put ("description", "Description of device" + String. valueOf (I); groupData. add (map); // initialize Child data ArrayList <Map <String, String> childDataList = new ArrayList <> (); for (int j = 0; j <4; j ++) {HashMap <String, String> tmpMap = new HashMap <String, String> (); tmpMap. put ("ability", "ability" + String. valueOf (I) + String. valueOf (j); childDataList. add (tmpMap);} childData. add (childDataLi St);}/* use four groups * Data to represent the expression of the group, and use four child * Data to represent the expression of the Child. GroupData. Each Map represents all the data to be displayed in a Group. For example, you want to display name, type, description on GroupTab, then you can use key-value pairs in Map to put these items. GroupLayout is just a view, which must contain all elements used to display projects such as name, type, description. These elements need to be placed in groupTo. The order must correspond to the key order in groupFrom. GroupFrom: the key of the Group data to be displayed. GroupTo: ID of the View corresponding to the Group data to be displayed. These IDS must be in the previously defined group layout. Similar to Child, each Map corresponds to all data items of a Child. The first List is divided into different groups, the second List is divided into different ChildItem, the third layer is Map, and each Child's data set. **/Export mArrayAdapter = new SimpleExpandableListAdapter (this, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo); list. setAdapter (mArrayAdapter );}}

 

GroupView: 
 
<?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="50dp"    android:background="@android:color/holo_blue_light"    android:orientation="horizontal">    <TextView        android:id="@+id/txt_view_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="10dp"        android:textSize="20dp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <TextView            android:id="@+id/txt_view_type"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20dp" />        <TextView            android:id="@+id/txt_view_description"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20dp" />    </LinearLayout></LinearLayout>
 

 

 

ChildView:

 
<?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="30dp"    android:orientation="vertical">    <TextView        android:id="@+id/txt_view_child_ability"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:textSize="18dp" /></LinearLayout>
 

 

 

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.