`
yidongkaifa
  • 浏览: 4068379 次
文章分类
社区版块
存档分类
最新评论

Android学习笔记(二五): 多信息显示-ExpandableListView的使用

 
阅读更多

如何在一个有限的屏幕上加载多页的信息,除此之外还可以通过隐藏-展开的方式,在屏幕有限的空间内包含更多的现象,如图所示,这就是ExpandableListView。

ExpandableListView,具有树的结构:Groups和childrens。下面我们通过一个简单的例子来学习,这个例子的数据不再采用String[],而是采用另一个常见的HashMap方式,顺带复习一下。

public class Chapter9Tutorial4extends ExpandableListActivity{
private static final String NAME="NAME";//这是HashMap的Key名称
private static final String IS_EVEN = "IS_EVEN";//这是HashMap的Key名称
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//步骤1:设置ExpandableListActivity的Group和Chlid的数据
//在这个例子中,Group的单元格式为HashMap<String,String>所以其格式为List<单元格式
//Child中最基础的item格式也为HashMap<String,String>,因此每一个Group中包含的child的格式为List<基础单元格式>,而所有的Group又是List,因此所有的child的格式为List<每个Group的child格式>,即List<List<基础单元格式>>
List<Map<String ,String>> groupData = new ArrayList<Map<String,String>>();
List<List<Map<String ,String>>> childData = new ArrayList<List<Map<String ,String>>>();

for(int i = 0 ; i< 6 ; i++){
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " +i);
curGroupMap.put(IS_EVEN, "Hello " + i);

List<Map<String,String>> children = new ArrayList<Map<String,String>>();
for(int j = 0; j < 3; j++){
Map<String ,String > curChildMap = new HashMap<String,String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j + " for group " + i);
curChildMap.put(IS_EVEN, "From Group " + i + " Hello my friend!");
}
childData.add(children);
}


//步骤2:设置ExpandableList的adapter,ExpandableListAdapter是个接口,对于Map方式可使用SimpleExpandableListAdapter
ExpandableListAdaptermAdapter = new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[]{NAME,IS_EVEN},
new int[] {android.R.id.text1,android.R.id.text2},
childData,
android.R.layout.simple_expandable_list_item_2,
new String[]{NAME,IS_EVEN},
new int[] {android.R.id.text1,android.R.id.text2}
);
setListAdapter(mAdapter);
}
//步骤3:触发处理,包括有:OnChildClickListener,OnGroupClickListener,OnGroupCollapseListener和OnGroupExpandListener。在这个例子中每个child的View的类型是TwoLineListItem

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Log.d("WEI","Click: [" + groupPosition + "," + childPosition + "] ");
return super.onChildClick(parent, v, groupPosition, childPosition, id);

}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics