您的位置:首页 > 移动开发 > Android开发

Android ExpandableListView的使用

2012-07-31 11:51 323 查看
展开型列表控件,原名ExpandableListView 是普通的列表控件进阶版, 可以自由的把列表进行收缩。

下面是实现后的截图:




一、界面设计

该控件的实现需要一个主界面main.xml、一个标题界面groups.xml、以及一个列表内容界面childs.xml。

(1)首先我们来看看main.xml主界面,只需要一个ExpandableListView即可:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

   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"

   />
</LinearLayout>

(2)标题界面groups.xml,只需要一个标题TextView控件上去即可。

<LinearLayout

   xmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"
>

   <TextView

       android:id="@+id/textGroup"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:paddingLeft="40px"

       android:paddingTop="6px"

       android:paddingBottom="6px"

       android:textSize="15sp"

       android:text="No data"

   />

</LinearLayout>

(3)列表内容控件childs.xml,是子控件,直接显示列表内容

<LinearLayout

   xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"
>

   <TextView 

       android:id="@+id/textChild"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:paddingLeft="60px"

       android:paddingTop="10px"

       android:paddingBottom="10px"

       android:textSize="20sp"

        android:text="No Data"
/>

</LinearLayout>

二、实现的主代码
下面给出具体的实现代码:

public
class ExpandActivity
extends ExpandableListActivity
{

   

   @Override

   public
void onCreate(Bundle
savedInstanceState)

   {

       
super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       

       
//创建二个一级条目标题

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

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

       

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

       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>();

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

       

       content_1.put("child",
"VC++");

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

       

       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>();

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

       

       content_3.put("child",
"敏捷开发");

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息