`

android ExpandableListActivity

 
阅读更多



 1:activity代码

 

package com.example.android20_expandablelistactivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.SimpleExpandableListAdapter;


public class MainActivity extends ExpandableListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 一级条目
        List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
        Map<String, String> group1 = new HashMap<String, String>();
        group1.put("group", "group1");
        Map<String, String> group2 = new HashMap<String, String>();
        group2.put("group", "group2");
        groups.add(group1);
        groups.add(group2);
      
        List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
        Map<String, String> child1Data1 = new HashMap<String, String>();
        child1Data1.put("child", "child1Data1");
        child1.add(child1Data1);
        Map<String, String> child1Data2 = new HashMap<String, String>();
        child1Data2.put("child", "child1Data2");
        child1.add(child1Data2);

        List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
        Map<String, String> child2Data1 = new HashMap<String, String>();
        child2Data1.put("child", "child2Data");
        child2.add(child2Data1);

        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
        childs.add(child1);
        childs.add(child2);

        SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
                this, groups, R.layout.group, new String[] { "group" },
                new int[] { R.id.groupTo }, childs, R.layout.child,
                new String[] { "child" }, new int[] { R.id.childTo });
        setListAdapter(sela);
    }

}

layout文件夹下main.xml  group.xml child.xml三个配置文件

 

main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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:drawSelectorOnTop="false" />


</LinearLayout>

 

 

group.xml 一级条目的控制显示格式的文件

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/groupTo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="10px"
        android:paddingLeft="60px"
        android:paddingTop="10px"
        android:text="No data"
        android:textSize="26sp" />

</LinearLayout>

 

child.xml 一级条目的控制显示格式的文件

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/childTo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="5px"
        android:paddingLeft="50px"
        android:paddingTop="5px"
        android:text="No data"
        android:textSize="20sp" />

</LinearLayout>

  • 大小: 18.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics