Android ExpandableListViewの簡単な応用

12897 ワード

Expandablelistview1Activity.java
 
package com.wangzhu.demoexpandablelistview;



import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;



import android.app.Activity;

import android.os.Bundle;

import android.widget.ExpandableListView;

import android.widget.SimpleExpandableListAdapter;



public class Expandablelistview1Activity extends Activity {



    private ExpandableListView expandableListView1;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.expandablelistview1);



        init();

    }



    private void init() {

        expandableListView1 = (ExpandableListView) findViewById(R.id.expandableListView1);

        getDatas();

    }



    private void getDatas() {

        //           

        Map<String, String> title_1 = new HashMap<String, String>();

        title_1.put("group", "    ");

        Map<String, String> title_2 = new HashMap<String, String>();

        title_2.put("group", "     ");



        //         

        List<Map<String, String>> groups = new ArrayList<Map<String, String>>();

        groups.add(title_1);

        groups.add(title_2);



        //         



        //    

        Map<String, String> content_1 = new HashMap<String, String>();

        content_1.put("child", "Android");

        Map<String, String> content_2 = new HashMap<String, String>();

        content_2.put("child", "Ios");



        List<Map<String, String>> childs_1 = new ArrayList<Map<String, String>>();

        childs_1.add(content_1);

        childs_1.add(content_2);



        //    

        Map<String, String> content_3 = new HashMap<String, String>();

        content_3.put("child", "  ");

        Map<String, String> content_4 = new HashMap<String, String>();

        content_4.put("child", "  ");

        Map<String, String> content_5 = new HashMap<String, String>();

        content_5.put("child", "  ");



        List<Map<String, String>> childs_2 = new ArrayList<Map<String, String>>();

        childs_2.add(content_3);

        childs_2.add(content_4);

        childs_2.add(content_5);



        //

        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

        childs.add(childs_1);

        childs.add(childs_2);



        /**

         *   1:     context   2:           3:           

         *   4:fromto,  map  key,           5:   4  ,      groups  id

         *   6:           7:              8:fromto,  map  key,        

         *   9:   8  ,      childs  id

         */

        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(

                this, groups, R.layout.expandablelistview1_groups,

                new String[] { "group" }, new int[] { R.id.textGroup }, childs,

                R.layout.expandablelistview1_child, new String[] { "child" },

                new int[] { R.id.textChild });

        expandableListView1.setAdapter(adapter);

    }



}

 
expandablelistview1.xml
<?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:orientation="vertical" >



    <ExpandableListView

        android:id="@+id/expandableListView1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

    </ExpandableListView>



</LinearLayout>

 
expandablelistview1_groups.xml
 
<?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:orientation="vertical" >



    <TextView

        android:id="@+id/textGroup"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:paddingBottom="6dp"

        android:paddingLeft="40dp"

        android:paddingTop="6dp"

        android:text="No data"

        android:textSize="15sp" />



</LinearLayout>

 
 
expandablelistview1_child.xml
<?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:orientation="vertical" >



    <TextView

        android:id="@+id/textChild"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:paddingBottom="10dp"

        android:paddingLeft="40dp"

        android:paddingTop="10dp"

        android:text="No data"

        android:textSize="20sp" />



</LinearLayout>

 
コメント:
簡単な応用、ネット上の導出はすべて見ることができて、理論は書かないで、直接ソースコードにしましょう!