ExpandableListView继承ListView,具有LIstVIew的基本功能。此外具有group/child,由组与子元素组成。

1.布局主要有是三个。

a.主布局:

<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expandlistview"
android:dividerHeight="5dp"
android:background="#ffffff"
android:divider="@drawable/expandchilddivide"
android:childDivider="#000000"
/>

b.Group布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/group_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:gravity="center_horizontal"
android:text="122"
/> </LinearLayout>

c.Child布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/textOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
/>
<TextView
android:id="@+id/textTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2"
/>
<TextView
android:id="@+id/textThree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="3"
/>
</LinearLayout>
</LinearLayout>

2.代码生成 类似adapter 创建adapter(ExpandableListAdapter、BaseExpandableListAdapter、SimpleExpandableListAdapter 依次继承关系)

a.创建adapter

//自定义适配器
class Adapter extends BaseExpandableListAdapter {
//获取子元素对象
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
} //获取子元素Id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} //加载子元素并显示
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = null;
ChildHolder childholder = null;
if (convertView != null) {
view = convertView;
childholder = (ChildHolder) view.getTag();
} else {
view = View.inflate(ExpandableListViewActi.this, R.layout.expand_child, null);
childholder = new ChildHolder();
//childholder.mImage = (ImageView) view.findViewById(R.id.image);
childholder.mPrice = (TextView) view.findViewById(R.id.textTwo);
childholder.mStateText = (TextView) view.findViewById(R.id.textOne);
childholder.mSecondPrice = (TextView) view.findViewById(R.id.textThree);
view.setTag(childholder);
}
// childholder.mImage.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(MainActivity.this, "第"+groupPosition+"组的第"+childPosition+"圖標被点击了", 0).show();
// }
// });
childholder.mPrice.setText(child_list.get(groupPosition));
int len = group_list.size();
System.out.println(len + "-----------------");
childholder.mStateText.setText(child_list.get(groupPosition));
childholder.mSecondPrice.setText(child_list.get(groupPosition));
return view;
} //获取子元素数目
@Override
public int getChildrenCount(int groupPosition) {
return child_list.size();
} //获取组元素对象
@Override
public Object getGroup(int groupPosition) {
return group_list.get(groupPosition);
} //获取组元素数目
@Override
public int getGroupCount() {
return group_list.size();
} //获取组元素Id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
} //加载并显示组元素
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = null;
GroupHolder groupholder = null;
if (convertView != null) {
view = convertView;
groupholder = (GroupHolder) view.getTag();
} else {
view = View.inflate(ExpandableListViewActi.this, R.layout.expand_group, null);
groupholder = new GroupHolder();
groupholder.mSpaceText = (TextView) view.findViewById(R.id.group_text);
view.setTag(groupholder);
}
groupholder.mSpaceText.setText(group_list.get(groupPosition));
return view;
} @Override
public boolean hasStableIds() { return true;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) { return true;
} }

b.生成主界面代码,设置adapter .expandable四中点击相应事件。

private void initView() {
mListView = (ExpandableListView) findViewById(R.id.expandlistview);
mInflater = LayoutInflater.from(ExpandableListViewActi.this);
group_list = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
group_list.add("zcx");
child_list.add("child");
}
Adapter adapter = new Adapter();
//mListView.setGroupIndicator(null); /**
* ExpandableListView的组监听事件
*/
// mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
//
// @Override
// public boolean onGroupClick(ExpandableListView parent, View v,
// int groupPosition, long id) {
// Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组被点击了", 0).show();
// return true;
// }
// });
/**
* ExpandableListView的组展开监听
*/
mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组展开", 0).show();
}
});
/**
* ExpandableListView的组合拢监听
*/
mListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组合拢", 0).show();
}
});
/**
* ExpandableListView的子元素点击监听
*/
mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组的第" + childPosition + "被点击了", 0).show();
return true;
}
}); mListView.setAdapter(adapter);
// int groupCount = mListView.getCount();
// for(int i=0;i<groupCount;i++){
// mListView.expandGroup(i);
// }
}

3.效果图

最新文章

  1. vue入门学习(基础篇)
  2. SQL Server基线算法(同比和环比)
  3. 第一个PHP程序-HelloWorld
  4. BZOJ1452——[JSOI2009]Count
  5. Java提高篇——Java实现多重继承
  6. CentOS linux系统搭建LAMP环境
  7. 关于求解不定方程的n(n-1)=2m(m-1)的解法的总结
  8. eclipse下添加viplugin插件的方法
  9. 好用的json-path
  10. Effective STL 中文版(大全)
  11. 使用generator自动生成Mybatis映射配置文件
  12. MySQL中/*!40100注释
  13. html中input type=file 改变样式
  14. bzoj 3551 [ONTAK2010]Peaks加强版(kruskal,主席树,dfs序)
  15. linux下fflush(stdin)的使用问题
  16. winform,wpf全屏 还显示任务栏的解决方法
  17. mysql5.7慢查询开启配置
  18. Effective MySQL之备份与恢复
  19. Java之面向对象例子(二)
  20. linux设备和驱动加载的先后顺序

热门文章

  1. 执行webpack-dev-server时,提示端口被占用。
  2. atnodes命令使用方法
  3. 性能测试day07_性能瓶颈和分析
  4. Android 开发 框架系列 Google的ORM框架 Room
  5. Win32API标准模板
  6. 解决:win10在空白处右键资源管理器重启的故障
  7. [C语言]进阶|数据类型: 整数, 浮点, 逻辑, 类型转换和条件运算
  8. Java中的包装数据类型
  9. MFC笔记3
  10. cdnbest区域自定义配置里添加防xss攻击配置