【原創】Android ExpandableListView使用

來源:互聯網
上載者:User

標籤:

ExpandableView的使用可以綁定到SimpleExpandableListAdapter,主要是看這個Adapter怎麼用。 這個類預設的建構函式有9個參數, 很好地解釋了什麼叫做又臭又長。
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)

不過不用擔心, 只要把Group開頭的和Child開頭的分成兩組來看就好。 具體看下面注釋部分。

代碼: 
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(Bundle 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 = {R.id.txt_view_name, R.id.txt_view_type, R.id.txt_view_description};        ArrayList<ArrayList<Map<String, String>>> childData = new ArrayList<>();        int childLayout = R.layout.group_view_child;        String[] childFrom = {"ability"};        int[] childTo = {R.id.txt_view_child_ability};        for (int i = 0; i < 3; i++) {            //初始化group資料            HashMap<String, String> map = new HashMap<>();            map.put("name", "device " + String.valueOf(i));            map.put("type", "type " + String.valueOf(i));            map.put("description", "Description of device " + String.valueOf(i));            groupData.add(map);            //初始化Child資料            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(childDataList);        }        /* 用四個group*資料來表示Group的表現方式,用四個child*的資料來表示Child的表示方式。        groupData,其中的每一個Map代表了一個Group的所有要顯示的資料,比如在GroupTab上你想顯示name,type,description,那麼就在Map裡面用索引值對放這幾項就好了。        groupLayout,只是一個視圖,其中要包含有所有用於顯示name,type,description等項目的元素。這些元素需要被放在groupTo裡面。並且順序要和groupFrom裡面的key順序對應。        groupFrom,要顯示的Group資料的key。        groupTo,要顯示的Group資料對應的View的ID。這些ID必須是之前定義的group布局裡面的。        Child基本上類似,也是每一個Map對應一個Child的所有資料項目。第一層List,分到不同的Group,第二層List,分到不同的ChildItem,第三層就是Map了,每個Child的資料集合。        * */        SimpleExpandableListAdapter 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>

 

 

【原創】Android ExpandableListView使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.