android 之 ExpandableListView列表中的列表,androidlistview列表

來源:互聯網
上載者:User

android 之 ExpandableListView列表中的列表,androidlistview列表

有時候,我們需要設計這樣一個介面,外面有一個列表,當我們點擊其中列表中的某個條目時,就會展開這個條目,出現一個新的列表。比如:(程式啟動並執行,在這裡貼出來)

當我們點擊第一項時,視圖變為:

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

為了實現這樣的效果,需要定義三個布局,包括顯示的main_activity(主布局),group(第一級列表布局),child(第二級列表布局),下面貼代碼,解釋都在注釋裡面:

main_activity:

<LinearLayout xmlns: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文檔中要求ExpandableListView的id必須為list,否則會拋出錯誤,有興趣的可以試試            #ff0000 為紅色        drawSelectorOntop=false是設定當你選中某一項時,任能清楚顯示當前項(我試了試好像沒有什麼改變。。。。)-->    <ExpandableListView         android:id="@android:id/list"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:drawSelectorOnTop="false"/>        <!-- 當上面沒有資料可以顯示時,就會顯示這個TextView。這個id也規定必須用empty,否者前面的話就沒作用了 -->    <TextView        android: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"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <!-- 這個是定義的第一級列表的布局    #0000ff是深青色 -->    <TextView         android: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"?><LinearLayout xmlns: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" >        <!-- 這裡定義的是列表中的第二級列表 ,當沒有資料時,顯示no data-->    <TextView         android: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>

接下來是主活動了,同樣解釋在代碼裡:

package com.example.expandlistaacitvity;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    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                //定義兩個一級條目        List<Map<String, String>> groups = new ArrayList<Map<String,String>>();        Map<String, String> group1 = new HashMap<String, String>();        group1.put("group", "group1第一級列表one");        groups.add(group1);        Map<String,String> group2 = new HashMap<String, String>();        group2.put("group", "group2第一級列表two");        groups.add(group2);                //定義一個list,裡面存放的是第一個一級條目中的二級條目        List<Map<String, String>> child1 = new ArrayList<Map<String,String>>();        Map<String, String> child1Date1 = new HashMap<String, String>();        child1Date1.put("child", "clild1date1第二級列表");        child1.add(child1Date1);        Map<String, String> child1Date2 = new HashMap<String, String>();        child1Date2.put("child", "child1Date2第二級列表");        child1.add(child1Date2);                //定義一個List,為第二個一級條目的二級條目        List<Map<String, String>> child2 = new ArrayList<Map<String,String>>();        Map<String, String> child2Date1 = new HashMap<String, String>();        child2Date1.put("child", "child222Date1第二級列表");        child2.add(child2Date1);                //將兩個二級條目放入到list中        List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>();        childs.add(child1);        childs.add(child2);                /*         * 1.上下文         * 2.一級列表的資料,包含各個group         * 3.指定一級列表的布局         * 4.指定一級列表的key         * 5.指定一級條目的控制項顯示的id         * 下面同上         */        SimpleExpandableListAdapter simpleExpandableListAdapter = 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(simpleExpandableListAdapter);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.