Android ApiDemos示例解析(112):Views->Expandable Lists->1. Custom Adapter


 

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, booleanisLastChild,
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 }

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值