股票场内基金交易,没时间盯盘?
简介
今天我们来看看 APIdemo中的 ExpandableListActivity,从名字来看,它是 Activity的子类,而且还与 Listview有关,Expandable是可张开的意思,那么他就是一个可张开的Listview。
特点
谷歌文档是这么说的:
ExpandableListActivity hosts a ExpandableListView object that can be bound to different data sources that provide a two-levels of data (the top-level is group, and below each group are children).
- 大概意思是 ExpandableListActivity持有 ExpandableListView对象,可以绑定不同的数据源来提供了两级的数据(第一级是组,每一组的下面是子组),也即是说它每个列表项有两个组,一个是主组,一个是它的子组。
如何使用
上面提到 ExpandableListActivity持有 ExpandableListView,要使用 ExpandableListView,就要为他提供一个 Adpter,这与Listview很类似,但也有不同,使用的是 BaseExpandableListAdapter, CursorTreeAdapter, ResourceCursorTreeAdapter, SimpleCursorTreeAdapter, SimpleExpandableListAdapter,谷歌文档给出的Demo就是使用BaseExpandableListAdapter,下面来看看源码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
public class ExpandableList1 extends ExpandableListActivity { ExpandableListAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set up our adapter mAdapter = new MyExpandableListAdapter(); setListAdapter(mAdapter); registerForContextMenu(getExpandableListView()); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("Sample menu"); menu.add(0, 0, 0, R.string.expandable_list_sample_action); } @Override public boolean onContextItemSelected(MenuItem item) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo(); String title = ((TextView) info.targetView).getText().toString(); int type = ExpandableListView.getPackedPositionType(info.packedPosition); if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos, Toast.LENGTH_SHORT).show(); return true; } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show(); return true; } return false; } public class MyExpandableListAdapter extends BaseExpandableListAdapter { //该数组是为列表项中的主组提供数据。 private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; //这个数组是为展开后的项,也就是子组提供数据的,请注意他是一个二维数组 private String[][] children = { { "Arnold", "Barry", "Chuck", "David" }, { "Ace", "Bandit", "Cha-Cha", "Deuce" }, { "Fluffy", "Snuggles" }, { "Goldy", "Bubbles" } }; public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition]; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public int getChildrenCount(int groupPosition) { return children[groupPosition].length; } //该方法是为ExpandableListView设置布局 public TextView getGenericView() { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(ExpandableList1.this); textView.setLayoutParams(lp); // Center the text vertically textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position textView.setPadding(36, 0, 0, 0); return textView; } //该方法控制子组的显示,也就是列表项中点击了某一项后,展开的项。 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } public Object getGroup(int groupPosition) { return groups[groupPosition]; } public int getGroupCount() { return groups.length; } public long getGroupId(int groupPosition) { return groupPosition; } //该方法控制主组的显示,也就是列表项中首先显示的列表 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); return textView; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } } } |
-
从上面可以看到,BaseExpandableListAdapter其实是一个抽象类,使用时需要实现其中的方法,
比较主要的方法有 getGroupView(XXX)方法和 getChildView(XX),这两个方法分别控制主组的显示视图和子组的显示视图,返回值都是View对象。 我们可以用 java代码来写布局,也可以通过布局填充的方式来加载(LayoutInflater ),Demo中是用的java代码。我个人觉得用 LayoutInflater比较方便。 -
在 BaseExpandableListAdapter需要定义两个数组,一个是为主组提供显示数据的一维数组,另一个是为展开项的子组提供数据的二位数组。
想获得去掉 5 元限制的证券账户吗?

如果您想去掉最低交易佣金 5 元限制,使用微信扫描左边小程序二维码,访问微信小程序「优财助手」,点击底部菜单「福利」,阅读文章「通过优财开证券账户无最低交易佣金 5 元限制」,按照文章步骤操作即可获得免 5 元证券账户,股票基金交易手续费率万 2.5。
请注意,一定要按照文章描述严格操作,如错误开户是无法获得免 5 元证券账户的。