星空网 > 软件开发 > 操作系统

【原创】Android ExpandableListView使用

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:
<??><LinearLayout ="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:

<??><LinearLayout ="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使用

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海外物流仓:https://www.goluckyvip.com/tag/33419.html
Listing:https://www.goluckyvip.com/tag/3342.html
海外物流仓储:https://www.goluckyvip.com/tag/33420.html
海外物流查询:https://www.goluckyvip.com/tag/33421.html
海外物流单号购买:https://www.goluckyvip.com/tag/33422.html
海外物流价格:https://www.goluckyvip.com/tag/33423.html
E-Bike品牌EMotorad获战略融资!:https://www.kjdsnews.com/a/1840707.html
Shopee订单量激增5倍多,越南成韩国美容品牌主要消费市场:https://www.kjdsnews.com/a/1840708.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流