ExpandableListView 神展开控件的初步使用

ExpandableListView 就是可以展开的ListView。

ListViewAndroid开发中最常见的列表控件,功能虽然多,但是也有不适合干的事儿,比如,当我们的列表需要下拉项的时候(省市选择,目录神展开啥的),ExpandableListView 是个绝佳的选择。

 

且看官方API

 

A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: 

groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view. 

功能写的很清楚,简单说就是:两级菜单,用ExpandableListAdapter 适配。

 

来看看适配方法

1.首先可以用

SimpleExpandableListAdapter.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)

适配

 

参数繁多,填进去就好

context  不解释
groupData  第一层菜单数据 
groupLayout  第一层菜单布局 
groupFrom  Map的key 
groupTo  value放到哪里去 
childData  第二层菜单数据 
childLayout  第二层菜单布局 
childFrom  Map的key 
childTo  value放到哪里去

 

 

 

 

 

 

 

2.或者可以用BaseExpandableListAdapter()

下面看看demo的例子,两种方法都有:

 

  1 package com.android.hj;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import android.app.ExpandableListActivity;
  9 import android.os.Bundle;
 10 import android.util.Log;
 11 import android.view.LayoutInflater;
 12 import android.view.View;
 13 import android.view.ViewGroup;
 14 import android.widget.BaseExpandableListAdapter;
 15 import android.widget.ExpandableListAdapter;
 16 import android.widget.ExpandableListView;
 17 import android.widget.SimpleExpandableListAdapter;
 18 import android.widget.TextView;
 19 
 20 public class MainActivity extends ExpandableListActivity {
 21     private static final String NAME = "NAME";
 22     private static final String IS_EVEN = "IS_EVEN";
 23     List<Map<String,String>> groupData;
 24     List<List<Map<String,String>>> childData;
 25     @Override
 26     public void onCreate(Bundle savedInstanceState) {
 27         super.onCreate(savedInstanceState);
 28       /*
 29            步骤1:设置ExpandableListActivity的Group和Chlid的数据
 30           在这个例子中,Group的单元格式为HashMap<String,String>所以其格式为List<单元格式
 31           Child中最基础的item格式也为HashMap<String,String>,
 32           因此每一个Group中包含的child的格式为List<基础单元格式>,而所有的Group又是List,
 33           因此所有的child的格式为List<每个Group的child格式>,即List<List<基础单元格式>>
 34       */
 35         groupData = new ArrayList<Map<String,String>>();
 36         childData = new ArrayList<List<Map<String,String>>>();
 37         
 38         for(int i=0;i<6;i++){
 39             Map<String,String> curGroupMap= new HashMap<String,String>();
 40             groupData.add(curGroupMap);
 41             curGroupMap.put(NAME, "Group" + i);
 42             curGroupMap.put(IS_EVEN, "child" + i);
 43             
 44             List<Map<String,String>> children = new ArrayList<Map<String,String>>();
 45             for(int j = 0;j<3;j++){
 46                 Map<String,String> curChildMap = new HashMap<String, String>();
 47                 children.add(curChildMap);
 48                 curChildMap.put(NAME, "Child"+j+"for group" +i);
 49                 curChildMap.put(IS_EVEN, "From Group "+ i +" YO,My Man,whaz up~");
 50             }
 51             childData.add(children);
 52         }
 53         
 54         /*步骤2:设置ExpandableList的adapter,ExpandableListAdapter是个接口,
 55          * 对于Map方式可使用SimpleExpandableListAdapter
 56         */
 57         ExpandableListAdapter mAdapter = new SimpleExpandableListAdapter(
 58                 getApplicationContext(), 
 59                 groupData, 
 60                 android.R.layout.simple_expandable_list_item_2, 
 61                 new String[]{NAME,IS_EVEN}, 
 62                 new int[]{android.R.id.text1,android.R.id.text1}, 
 63                 childData, 
 64                 android.R.layout.simple_expandable_list_item_2, 
 65                 new String[]{NAME,IS_EVEN}, 
 66                 new int[]{android.R.id.text1,android.R.id.text2}
 67         );
 68         ExpandableListAdapter mAdapter2 = new BaseExpandableListAdapter() {
 69             
 70             @Override
 71             public boolean isChildSelectable(int groupPosition, int childPosition) {
 72                 Log.i("fuck","isChildSelectable:Click: [" + groupPosition + "," + childPosition + "]");
 73                 return true;
 74             }
 75             
 76             @Override
 77             public boolean hasStableIds() {
 78                 Log.i("fuck", "hasStableIds");
 79                 return false;
 80             }
 81             
 82             @Override
 83             public View getGroupView(int groupPosition, boolean isExpanded,
 84                     View convertView, ViewGroup parent) {
 85                 Log.i("fuck", "getGroupView");
 86                 LayoutInflater inflater = getLayoutInflater();
 87                 convertView = inflater.inflate(android.R.layout.simple_expandable_list_item_1, null);
 88                 TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
 89                 textView.setText("Group");
 90                 Log.i("fuck", textView.getPaddingLeft()+"");
 91                 return convertView;
 92             }
 93             
 94             @Override
 95             public long getGroupId(int groupPosition) {
 96                 Log.i("fuck", "getGroupId");
 97                 return groupPosition;
 98             }
 99             
100             @Override
101             public int getGroupCount() {
102                 Log.i("fuck", "getGroupCount");
103                 return groupData.size();
104             }
105             
106             @Override
107             public Object getGroup(int groupPosition) {
108                 Log.i("fuck", "getGroup");
109                 return null;
110             }
111             
112             @Override
113             public int getChildrenCount(int groupPosition) {
114                 Log.i("fuck", "getChildrenCount");
115                 return childData.size();
116             }
117             
118             @Override
119             public View getChildView(int groupPosition, int childPosition,
120                     boolean isLastChild, View convertView, ViewGroup parent) {
121                 Log.i("fuck", "getChildView");
122                 LayoutInflater inflater = getLayoutInflater();
123                 convertView = inflater.inflate(android.R.layout.simple_expandable_list_item_2, null);
124                 TextView textView1 = (TextView) convertView.findViewById(android.R.id.text1);
125                 TextView textView2 = (TextView) convertView.findViewById(android.R.id.text2);
126                 textView1.setText("child");
127                 textView1.setTextSize(20);
128                 textView2.setText("holly shit!");
129                 //textView2.setTextColor(android.R.attr.textColorSecondaryInverse);
130                 return convertView;
131             }
132             
133             @Override
134             public long getChildId(int groupPosition, int childPosition) {
135                 Log.i("fuck", "getChildId");
136                 return childPosition;
137             }
138             
139             @Override
140             public Object getChild(int groupPosition, int childPosition) {
141                 Log.i("fuck", "getChild");
142                 return null;
143             }
144         };
145         setListAdapter(mAdapter);
146         
147     }
148     /*
149       步骤3:触发处理,包括有:OnChildClickListener,OnGroupClickListener,
150     OnGroupCollapseListener和OnGroupExpandListener。
151      在这个例子中每个child的View的类型是TwoLineListItem
152      */
153     @Override
154     public boolean onChildClick(ExpandableListView parent, View v,
155             int groupPosition, int childPosition, long id) {
156         Log.i("fuck","Click: [" + groupPosition + "," + childPosition + "] ");
157         return super.onChildClick(parent, v, groupPosition, childPosition, id);
158     }
159 }

 

想看第二种方法,只要把setListAdapter(mAdapter)换成setListAdapter(mAdapter2)。(黑体字部分)

这个demo我故意没有用自己的界面,item用的全是Android预定义的布局。

>_<

 

 

 

 

 

posted @ 2012-11-28 17:01  黄泉hj  阅读(2171)  评论(0编辑  收藏  举报
沪江在线词典