標籤:
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使用