zoukankan      html  css  js  c++  java
  • ExpandableList的用法

    代码
    1 package com.android.osb;
    2
    3 import android.app.ExpandableListActivity;
    4 import android.os.Bundle;
    5 import android.view.ContextMenu;
    6 import android.view.ContextMenu.ContextMenuInfo;
    7 import android.view.Gravity;
    8 import android.view.MenuItem;
    9 import android.view.View;
    10 import android.view.ViewGroup;
    11 import android.view.ViewGroup.LayoutParams;
    12 import android.widget.AbsListView;
    13 import android.widget.BaseExpandableListAdapter;
    14 import android.widget.ExpandableListAdapter;
    15 import android.widget.ExpandableListView;
    16 import android.widget.Toast;
    17 import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
    18 import android.widget.TextView;
    19
    20 public class ExpandableList extends ExpandableListActivity {
    21
    22 private ExpandableListAdapter expandableListAdapter;
    23
    24 @Override
    25 protected void onCreate(Bundle savedInstanceState) {
    26 // TODO Auto-generated method stub
    27 super.onCreate(savedInstanceState);
    28 expandableListAdapter = new MyExpandable();
    29
    30 //设置ListAdapter的Adapter为继承自BaseExpandableListAdapter的一个Adapter
    31 setListAdapter(expandableListAdapter);
    32
    33 //注册长按菜单
    34 registerForContextMenu(this.getExpandableListView());
    35 }
    36
    37 @Override
    38 public void onCreateContextMenu(ContextMenu menu, View v,
    39 ContextMenuInfo menuInfo) {
    40 // TODO Auto-generated method stub
    41
    42 //弹出的长按菜单的标题
    43 menu.setHeaderTitle("ContextMenu");
    44
    45 //弹出的长按菜单的选项
    46 menu.add(0, 0, 0, "ContextMenu");
    47 }
    48
    49
    50 //点击长按菜单选项的处理事件
    51 @Override
    52 public boolean onContextItemSelected(MenuItem item) {
    53 // TODO Auto-generated method stub
    54
    55 //从MenuItem中拿到ContextMenuInfo,并强制转换成ExpandableListContextMenuInfo.
    56 ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
    57 //将info中的contextMenu正在显示的View的标题拿到,并强制转换成TextView.
    58 String title = ((TextView)info.targetView).getText().toString();
    59
    60 //从info中拿到packedPosition,并根据packedPosition判断ExpandableListView的类型,是child还是group。
    61 int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    62 if(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
    63 int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    64 int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
    65 //如果是child,从ExpandableListView中拿到groupPos和childPos.并显示出来。
    66 Toast.makeText(this, "title : " + title + ", groupPos : " + groupPos + ", childPos : " + childPos, Toast.LENGTH_LONG).show();
    67 return true;
    68 } else if(type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
    69 int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    70 //如果是group,从ExpandableListView中拿到groupPos并显示出来。
    71 Toast.makeText(this, "title : " + title + ", groupPos : " + groupPos, Toast.LENGTH_LONG).show();
    72 return true;
    73 }
    74 return false;
    75 }
    76
    77 //这个Adapter是继承BaseExpandableListAdapter的。
    78 private class MyExpandable extends BaseExpandableListAdapter {
    79
    80 //父列表数据
    81 private String[] groups = {
    82 "group1",
    83 "group2",
    84 "group3",
    85 "group4"
    86 };
    87
    88 //子列表数据
    89 private String[][] children = {
    90 {"child1"},
    91 {"child1","child2"},
    92 {"child1", "child2", "child3"},
    93 {"child1", "child2", "child3", "child4"}
    94 };
    95
    96 //根据groupPosition和childPosition拿到child的对象
    97 @Override
    98 public Object getChild(int groupPosition, int childPosition) {
    99 // TODO Auto-generated method stub
    100 return children[groupPosition][childPosition];
    101 }
    102
    103
    104 //根据childPosition拿到child的ID
    105 @Override
    106 public long getChildId(int groupPosition, int childPosition) {
    107 // TODO Auto-generated method stub
    108 return childPosition;
    109 }
    110
    111 @Override
    112 public View getChildView(int groupPosition, int childPosition, boolean isLastchild, View convertView,
    113 ViewGroup parent) {
    114 // TODO Auto-generated method stub
    115 //创建一个TextView,并将child对象转换成字符串显示在TextView上面。
    116 TextView tv = getGenericView();
    117 tv.setText(getChild(groupPosition, childPosition).toString());
    118 return tv;
    119 }
    120
    121 //根据groupPosition拿到children的长度。
    122 @Override
    123 public int getChildrenCount(int groupPosition) {
    124 // TODO Auto-generated method stub
    125 return children[groupPosition].length;
    126 }
    127
    128 //根据groupPosition拿到group的对象
    129 @Override
    130 public Object getGroup(int groupPosition) {
    131 // TODO Auto-generated method stub
    132 return groups[groupPosition];
    133 }
    134
    135 @Override
    136 public int getGroupCount() {
    137 // TODO Auto-generated method stub
    138 return groups.length;
    139 }
    140
    141 @Override
    142 public long getGroupId(int groupPosition) {
    143 // TODO Auto-generated method stub
    144 return groupPosition;
    145 }
    146
    147 @Override
    148 public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
    149 ViewGroup parent) {
    150 // TODO Auto-generated method stub
    151 //创建一个TextView,并将group对象转换成字符串显示在TextView上面。
    152 TextView tv = getGenericView();
    153 tv.setText(getGroup(groupPosition).toString());
    154 return tv;
    155 }
    156
    157 @Override
    158 public boolean hasStableIds() {
    159 // TODO Auto-generated method stub
    160 return true;
    161 }
    162
    163 @Override
    164 public boolean isChildSelectable(int groupPosition, int childPosition) {
    165 // TODO Auto-generated method stub
    166 return true;
    167 }
    168
    169 //创建一个新的TextView对象。
    170 private TextView getGenericView () {
    171 TextView tv = new TextView(ExpandableList.this);
    172 tv.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    173 tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
    174 tv.setPadding(32, 0, 0, 0);
    175 return tv;
    176 }
    177 }
    178 }
    179

  • 相关阅读:
    黑松白鹿
    跨越
    第三年
    Lua windows环境搭建
    Iron man
    水果沙拉
    六周岁
    sqlserver数据库附加报错5120
    [BeiJing2006]狼抓兔子 平面图最小割
    BZOJ2118: 墨墨的等式 思维建图
  • 原文地址:https://www.cnblogs.com/shang53880/p/1934417.html
Copyright © 2011-2022 走看看