引用:http://note.youdao.com/share/?id=994df799c2dcc8d83a8909173e42f80d&type=note

先看效果,过瘾一番。

源码下载:http://files.cnblogs.com/salam/WidgetDemo.rar

  
  ExpandableListView是Android中的手风琴,本人感觉效果相当棒。

  一、ExpandableListView介绍

    一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。

  1.重要方法

      expandGroup(int groupPos) :在分组列表视图中展开一组,

      setSelectedGroup(int groupPosition) :设置选择指定的组。

      setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :设置选择指定的子项。

      getPackedPositionGroup(long packedPosition) :返回所选择的组

      getPackedPositionForChild(int groupPosition, int childPosition) :返回所选择的子项

      getPackedPositionType(long packedPosition) :返回所选择项的类型(Child,Group)

      isGroupExpanded(int groupPosition) :判断此组是否展开

  2.代码:

ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
String title=((TextView)menuInfo.targetView).getText().toString();
int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);

if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);

二、ExpandableListAdapter

    一个接口,将基础数据链接到一个ExpandableListView。此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。

  1.重要方法

    getChildId(int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。

    getChildrenCount(int groupPosition) 返回在指定Group的Child数目。

  2.代码

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
public String[] groups = { "我的好友", "新疆同学", "亲戚", "同事" };
public String[][] children = {
{ "胡算林", "张俊峰", "王志军", "二人" },
{ "李秀婷", "蔡乔", "别高", "余音" },
{ "摊派新", "张爱明" },
{ "马超", "司道光" }
};

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;
}

public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);

TextView textView = new TextView(ExpandableListDemo.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;
}

}

更多相关文章

  1. Android(安卓)环境搭建
  2. android studio调试c/c++代码
  3. android拍照与读取相册
  4. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  5. Android(安卓)命令行编译、打包生成apk文件
  6. Android内容提供者源码
  7. android 单元测试
  8. 【安卓笔记】android客户端与服务端交互的三种方式
  9. android背景选择器selector用法汇总

随机推荐

  1. Android(安卓)2.3 StatusBar 分析(一)
  2. Android(安卓)SDK Android(安卓)NDK 官方
  3. Android(安卓)全退出
  4. Android(安卓)Factory Reset flow
  5. 收集android的三个小tip
  6. 【黑马Android】(02)短信发送器/布局演示
  7. listView 多值显示
  8. Android(安卓)RecyclerView添加分隔线 Di
  9. android 键盘显示与隐藏
  10. Android(安卓)Hook学习之ptrace函数的使