A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈君 金牌黑马   /  2014-9-20 21:18  /  1327 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. ActivityMain .java

  2. package study.com.android;

  3. import android.app.ExpandableListActivity;
  4. import android.os.Bundle;
  5. import android.view.ContextMenu;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import android.view.ContextMenu.ContextMenuInfo;
  9. import android.widget.ExpandableListAdapter;
  10. import android.widget.ExpandableListView;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

  14. public class ActivityMain extends ExpandableListActivity {

  15. private ExpandableListAdapter mAdapter;

  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. this.setTitle("ExpandableList");
  20. mAdapter = new MyExpandableListAdapter(this);
  21. setListAdapter(mAdapter);
  22. registerForContextMenu(this.getExpandableListView());
  23. }

  24. // 为列表的每一项创建上下文菜单(即长按后 呼出的菜单)
  25. @Override
  26. public void onCreateContextMenu(ContextMenu menu, View v,
  27. ContextMenuInfo menuInfo) {
  28. menu.setHeaderTitle("ContexMenu");
  29. menu.add(0, 0, 0, "ContextMenu");
  30. }

  31. // 单击上下文菜单后的逻辑
  32. @Override
  33. public boolean onContextItemSelected(MenuItem item) {

  34. ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item
  35. .getMenuInfo();
  36. String title = ((TextView) info.targetView).getText().toString();

  37. int type = ExpandableListView
  38. .getPackedPositionType(info.packedPosition);
  39. if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {

  40. int groupPos = ExpandableListView
  41. .getPackedPositionGroup(info.packedPosition);
  42. int childPos = ExpandableListView
  43. .getPackedPositionChild(info.packedPosition);
  44. Toast.makeText(
  45. this,
  46. title + "-Group Index" + groupPos + "Child Index:"
  47. + childPos, Toast.LENGTH_SHORT).show();
  48. return true;
  49. }
  50. return false;
  51. }
  52. }
  53. MyExpandableListAdapter.java

  54. package study.com.android;

  55. import android.content.Context;
  56. import android.view.Gravity;
  57. import android.view.View;
  58. import android.view.ViewGroup;
  59. import android.widget.AbsListView;
  60. import android.widget.BaseExpandableListAdapter;
  61. import android.widget.TextView;

  62. public class MyExpandableListAdapter extends BaseExpandableListAdapter {

  63. private Context mContext;

  64. // 父列表数据
  65. private String[] groups = { "group1", "group2", "group3", "group4", "" };

  66. // 子列表数据
  67. private String[][] children = { { "child1" }, { "child1", "child2" },
  68. { "child1", "child2", "child3" },
  69. { "child1", "child2", "child3", "child4" }, };

  70. MyExpandableListAdapter(Context context) {
  71. mContext = context;
  72. }

  73. @Override
  74. public Object getChild(int groupPosition, int childPosition) {
  75. // TODO Auto-generated method stub
  76. return children[groupPosition][childPosition];
  77. }

  78. @Override
  79. public long getChildId(int groupPosition, int childPosition) {
  80. // TODO Auto-generated method stub
  81. return childPosition;
  82. }

  83. // 取子列表中的某一项的view
  84. @Override
  85. public View getChildView(int groupPosition, int childPosition,
  86. boolean isLastChild, View convertView, ViewGroup parent) {
  87. TextView textView = getGenericView();
  88. ;
  89. textView.setText(getChild(groupPosition, childPosition).toString());
  90. return textView;
  91. }

  92. @Override
  93. public int getChildrenCount(int groupPosition) {
  94. // TODO Auto-generated method stub
  95. return children[groupPosition].length;
  96. }

  97. @Override
  98. public Object getGroup(int groupPosition) {
  99. return groups[groupPosition];
  100. }

  101. @Override
  102. public int getGroupCount() {
  103. // TODO Auto-generated method stub
  104. return groups.length;
  105. }

  106. @Override
  107. public long getGroupId(int groupPosition) {
  108. // TODO Auto-generated method stub
  109. return groupPosition;
  110. }

  111. @Override
  112. public View getGroupView(int groupPosition, boolean isExpanded,
  113. View convertView, ViewGroup parent) {
  114. TextView textView = getGenericView();
  115. textView.setText(getGroup(groupPosition).toString());

  116. return textView;
  117. }

  118. @Override
  119. public boolean hasStableIds() {
  120. // TODO Auto-generated method stub
  121. return true;
  122. }

  123. @Override
  124. public boolean isChildSelectable(int groupPosition, int childPosition) {
  125. // TODO Auto-generated method stub
  126. return true;
  127. }

  128. // 获取某一项的view的逻辑
  129. private TextView getGenericView() {
  130. AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
  131. ViewGroup.LayoutParams.FILL_PARENT, 48);
  132. TextView textView = new TextView(mContext);
  133. textView.setLayoutParams(lp);
  134. textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
  135. textView.setPadding(32, 0, 0, 0);
  136. return textView;
  137. }

  138. }
复制代码
运行的结果如下:

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马