Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter
来源:互联网 发布:数据统计与分析与ai 编辑:程序博客网 时间:2024/05/07 13:50
Expandable List 与普通的List的区别在于Expandable List可以显示两个层次,第一层称为”Group” 第二层称为”Child” 。其中”Child”可以折叠或者展开,有点像只支持两級的TreeView(文件游览器)。
用法上和List 非常类似,下面的对应关系可以帮助理解Expandable List
- ExpandableListActivity –>ListActivity
- ExpandableListView –> ListView
- ExpandableListAdapter –>ListAdapter
ExpandableListActivity 预设的Layout 為一居中全屏显示的列表(两级)。Android也允許使用自定义的Layout来显示Expandable List. 为了使用自定义的Layout的,在XML中必须包括一個ExpandableListView对象且id必须为 @android:id/list。 此外也可以为空列表定义一個View,此View的id为 @id/android:empty。
如下例为ExpandableListActivity 自定义一個Layout ,为在列表为空时显示「No Data”.可以在onCreate 使用setContentView设置這個自定义Layout .
<?xml version=”1.0″ encoding=”UTF-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingLeft=”8dp”
android:paddingRight=”8dp”>
<ExpandableListView android:id=”@id/android:list”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#00FF00″
android:layout_weight=”1″
android:drawSelectorOnTop=”false”/>
<TextView android:id=”@id/android:empty”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#FF0000″
android:text=”No data”/>
</LinearLayout>
ExpandableListActivity 调用setListAdapter(ExpandableListAdapter) 设置ExpandableListAdapter, ExpandableListAdapter 允许为”group” 和 “child”分別设置View。
例如本例 MyExpandableListAdapter 派生自BaseExpandableListAdapter,重载了getChildView 和getGroupView,分別为group 和child定义一個TextView,TextView的文本为Group名称或是Child的名称,分別对应在groups 和children。
1
// Sample data set. children[i] contains
2
//the children (String[]) for groups[i].
3
private
String[] groups = {
"People Names"
,
"Dog Names"
,
4
"Cat Names"
,
"Fish Names"
};
5
private
String[][] children = {
6
{
"Arnold"
,
"Barry"
,
"Chuck"
,
"David"
},
7
{
"Ace"
,
"Bandit"
,
"Cha-Cha"
,
"Deuce"
},
8
{
"Fluffy"
,
"Snuggles"
},
9
{
"Goldy"
,
"Bubbles"
}
10
};
11
12
public
View getChildView(
int
groupPosition,
int
childPosition,
boolean
isLastChild,
13
View convertView, ViewGroup parent) {
14
TextView textView = getGenericView();
15
textView.setText(getChild(groupPosition, childPosition).toString());
16
return
textView;
17
}
18
19
public
View getGroupView(
int
groupPosition,
boolean
isExpanded, View convertView,
20
ViewGroup parent) {
21
TextView textView = getGenericView();
22
textView.setText(getGroup(groupPosition).toString());
23
return
textView;
24
}
1
// Sample data set. children[i] contains
2
//the children (String[]) for groups[i].
3
private
String[] groups = {
"People Names"
,
"Dog Names"
,
4
"Cat Names"
,
"Fish Names"
};
5
private
String[][] children = {
6
{
"Arnold"
,
"Barry"
,
"Chuck"
,
"David"
},
7
{
"Ace"
,
"Bandit"
,
"Cha-Cha"
,
"Deuce"
},
8
{
"Fluffy"
,
"Snuggles"
},
9
{
"Goldy"
,
"Bubbles"
}
10
};
11
12
public
View getChildView(
int
groupPosition,
int
childPosition,
boolean
isLastChild,
13
View convertView, ViewGroup parent) {
14
TextView textView = getGenericView();
15
textView.setText(getChild(groupPosition, childPosition).toString());
16
return
textView;
17
}
18
19
public
View getGroupView(
int
groupPosition,
boolean
isExpanded, View convertView,
20
ViewGroup parent) {
21
TextView textView = getGenericView();
22
textView.setText(getGroup(groupPosition).toString());
23
return
textView;
24
}
getExpandableListView() 取得当前Activity 对应的ExpandableListView, 然后调用registerForContextMenu 为 ExpandableListView 添加Context Menu.
然後在onContextItemSelected事件中为List View 中的每個list Item 添加事件,本例使用Toast 显示一行文本,显示当前点击的事件。
1
public
boolean
onContextItemSelected(MenuItem item) {
2
ExpandableListContextMenuInfo info
3
= (ExpandableListContextMenuInfo) item.getMenuInfo();
4
5
String title = ((TextView) info.targetView).getText().toString();
6
7
int
type = ExpandableListView.getPackedPositionType(info.packedPosition);
8
if
(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
9
int
groupPos
10
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
11
int
childPos
12
= ExpandableListView.getPackedPositionChild(info.packedPosition);
13
Toast.makeText(
this
, title +
": Child "
+ childPos
14
+
" clicked in group "
+ groupPos,
15
Toast.LENGTH_SHORT).show();
16
return
true
;
17
}
else
if
(type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
18
int
groupPos
19
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
20
Toast.makeText(
this
, title +
": Group "
+ groupPos
21
+
" clicked"
, Toast.LENGTH_SHORT).show();
22
return
true
;
23
}
24
25
return
false
;
26
}
1
public
boolean
onContextItemSelected(MenuItem item) {
2
ExpandableListContextMenuInfo info
3
= (ExpandableListContextMenuInfo) item.getMenuInfo();
4
5
String title = ((TextView) info.targetView).getText().toString();
6
7
int
type = ExpandableListView.getPackedPositionType(info.packedPosition);
8
if
(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
9
int
groupPos
10
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
11
int
childPos
12
= ExpandableListView.getPackedPositionChild(info.packedPosition);
13
Toast.makeText(
this
, title +
": Child "
+ childPos
14
+
" clicked in group "
+ groupPos,
15
Toast.LENGTH_SHORT).show();
16
return
true
;
17
}
else
if
(type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
18
int
groupPos
19
= ExpandableListView.getPackedPositionGroup(info.packedPosition);
20
Toast.makeText(
this
, title +
": Group "
+ groupPos
21
+
" clicked"
, Toast.LENGTH_SHORT).show();
22
return
true
;
23
}
24
25
return
false
;
26
}
- Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter
- Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter
- Android ApiDemos示例解析(114):Views->Expandable Lists->3. SimpleAdapter
- Android ApiDemos示例解析(180):Views->Lists->13. Slow Adapter
- Android ApiDemos示例解析(181):Views->Lists->14.Efficient Adapter
- Android ApiDemos示例解析(113):Views->Expandable Lists->2. Cursor(People)
- Android ApiDemos示例解析(109):Views->Custom
- Android ApiDemos示例解析(109):Views->Custom
- Android ApiDemos示例解析(109):Views->Custom
- Android ApiDemos示例解析(168):Views->Lists->1. Array
- Android ApiDemos示例解析(169):Views->Lists->2. Cursor(People)
- Android ApiDemos示例解析(170):Views->Lists->3. Cursor (Phones)
- Android ApiDemos示例解析(171):Views->Lists->4. ListAdapter
- Android ApiDemos示例解析(172):Views->Lists->5. Separators
- Android ApiDemos示例解析(173):Views->Lists->6. ListAdapter Collapsed
- Android ApiDemos示例解析(174):Views->Lists->7. Cursor(Phones)
- Android ApiDemos示例解析(175):Views->Lists->8. Photos
- Android ApiDemos示例解析(176):Views->Lists->9. Array (Overlay)
- 计算机数学和算法习题和解答(1~3)
- mvc使用工厂类,实现真正的分离
- 【转】Why Memory Barrier?
- JPEG编解码器的简要仿真(无代码,无真相)
- 旧工程适配iOS 6和iPhone 5之第三方静态库
- Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter
- MYSQL的授权远程登录
- 空间数据库之矢量数据上载方法
- ffmpeg commands
- Trie树/字典树的简介及实现
- Android之-从执行ListView的removeFooterView时报错学到的东西(转)
- JavaEE Web应用开发平台WebBuilder开发团队专访
- 如何打开blackberry的界面
- 透明度设置