Android ExpandableListView 带有Checkbox的简单应用
expandablelistview2_groups.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/imageView1"
android:focusable="false"
android:focusableInTouchMode="false" /> </RelativeLayout>
expandablelistview2_child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" /> </RelativeLayout>
Expandablelistview2Activity.java
package com.wangzhu.demoexpandablelistview; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class Expandablelistview2Activity extends Activity { private final static String TAG = "Expandablelistview2Activity";
/**
* 定义组数据
*/
private List<String> groupDataList;
/**
* 定义组中的子数据
*/
private List<List<String>> childDataList; private ExpandableListView expandableListView;
private ExpandableAdapter adapter; /**
* 0:不可选 1:未选中 2:半选 3:选中
*/
private List<Map<Integer, Integer>> isSelectedList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandablelistview1);
loadData();
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1); // expandableListView.setGroupIndicator(getResources().getDrawable(
// R.drawable.indicator_selector)); expandableListView.setGroupIndicator(null);
adapter = new ExpandableAdapter();
expandableListView.setAdapter(adapter);
expandableListView
.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override
public void onGroupExpand(int groupPosition) {
// 只展开一个
for (int i = 0, count = expandableListView.getCount(); i < count; i++) {
if (groupPosition != i) {
expandableListView.collapseGroup(i);
}
}
}
}); expandableListView
.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override
public boolean onChildClick(ExpandableListView parent,
View v, int groupPosition, int childPosition,
long id) {
Toast.makeText(
Expandablelistview2Activity.this,
"onChildClick===="
+ adapter.getChild(groupPosition,
childPosition) + "===="
+ v.getParent(), Toast.LENGTH_LONG)
.show();
ViewHolder viewHolder = (ViewHolder) v.getTag();
viewHolder.checkBox.toggle();
if (viewHolder.checkBox.isChecked()) {
isSelectedList.get(groupPosition).put(
childPosition, 3);
} else {
isSelectedList.get(groupPosition).put(
childPosition, 1);
}
int count = 0;
for (int i = 0, size = isSelectedList
.get(groupPosition).size(); i < size; i++) {
if (isSelectedList.get(groupPosition).get(i) == 3) {
count++;
}
}
View view = (View) v.getParent();
Log.d(TAG, "view=" + view.findViewById(R.id.checkBox1));
CheckBox ck = (CheckBox) view
.findViewById(R.id.checkBox1);
if (count == isSelectedList.get(groupPosition).size()) {
// ck.setBackgroundResource(R.drawable.btn_select);
ck.setButtonDrawable(R.drawable.btn_select);
} else if (count > 0) {
// ck.setBackgroundResource(R.drawable.btn_half);
ck.setButtonDrawable(R.drawable.btn_half);
} else {
// ck.setBackgroundResource(R.drawable.btn_unselect);
ck.setButtonDrawable(R.drawable.btn_unselect);
}
adapter.notifyDataSetChanged();
return false;
}
}); isSelectedList = new ArrayList<Map<Integer, Integer>>();
for (int i = 0, icount = expandableListView.getCount(); i < icount; i++) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int j = 0, jcount = childDataList.get(i).size(); j < jcount; j++) {
map.put(j, 1);
}
isSelectedList.add(map);
}
} /**
* 全部展开或关闭
*
* @param flag
*/
private void showOrhide(boolean flag) {
for (int i = 0, count = expandableListView.getCount(); i < count; i++) {
if (flag) {
expandableListView.expandGroup(i);
} else {
expandableListView.collapseGroup(i);
}
}
} private void loadData() {
groupDataList = new ArrayList<String>();
groupDataList.add("国家");
groupDataList.add("人物");
groupDataList.add("武器"); childDataList = new ArrayList<List<String>>(); List<String> child1 = new ArrayList<String>();
child1.add("魏国");
child1.add("蜀国");
child1.add("吴国");
childDataList.add(child1); List<String> child2 = new ArrayList<String>();
child2.add("关羽");
child2.add("张飞");
child2.add("典韦");
child2.add("吕布");
child2.add("曹操");
child2.add("甘宁");
child2.add("郭嘉");
child2.add("周瑜");
childDataList.add(child2); List<String> child3 = new ArrayList<String>();
child3.add("青龙偃月刀");
child3.add("丈八蛇矛枪");
child3.add("青钢剑");
child3.add("麒麟弓");
child3.add("银月枪");
childDataList.add(child3);
} private class ExpandableAdapter extends BaseExpandableListAdapter { @Override
public int getGroupCount() {
return groupDataList.size();
} @Override
public int getChildrenCount(int groupPosition) {
return childDataList.get(groupPosition).size();
} @Override
public Object getGroup(int groupPosition) {
return groupDataList.get(groupPosition);
} @Override
public Object getChild(int groupPosition, int childPosition) {
return childDataList.get(groupPosition).get(childPosition);
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) { Log.d(TAG, "getGroupView");
ViewHolder viewHolder = null;
if (null == convertView) {
convertView = View.inflate(Expandablelistview2Activity.this,
R.layout.expandablelistview2_groups, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView
.findViewById(R.id.textView1);
viewHolder.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
viewHolder.imageView = (ImageView) convertView
.findViewById(R.id.imageView1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
} viewHolder.textView.setText(groupDataList.get(groupPosition));
viewHolder.checkBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int flag = 0;
if (isChecked) {
flag = 3;
} else {
flag = 1;
}
for (int i = 0, size = isSelectedList.get(
groupPosition).size(); i < size; i++) {
isSelectedList.get(groupPosition).put(i, flag);
}
notifyDataSetChanged();
}
});
int size = 0;
for (int i = 0, count = isSelectedList.get(groupPosition).size(); i < count; i++) {
if (isSelectedList.get(groupPosition).get(i) == 3) {
size++;
}
}
if (size == isSelectedList.get(groupPosition).size()) {
// viewHolder.checkBox
// .setBackgroundResource(R.drawable.btn_select);
viewHolder.checkBox.setButtonDrawable(R.drawable.btn_select);
} else if (size > 0) {
// viewHolder.checkBox.setBackgroundResource(R.drawable.btn_half);
viewHolder.checkBox.setButtonDrawable(R.drawable.btn_half);
} else {
// viewHolder.checkBox
// .setBackgroundResource(R.drawable.btn_unselect);
viewHolder.checkBox.setButtonDrawable(R.drawable.btn_unselect);
} // 判断isExpanded就可以控制是按下还是关闭,同时更换图片
if (isExpanded) {
viewHolder.imageView
.setBackgroundResource(R.drawable.shangiantou);
} else {
viewHolder.imageView
.setBackgroundResource(R.drawable.xiajiantou);
} return convertView;
} @Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) { Log.d(TAG, "getChildView");
// TextView textView = null;
// if (null != convertView) {
// textView = (TextView) convertView;
// textView.setText(childDataList.get(groupPosition).get(
// childPosition));
// } else {
// textView = createView(childDataList.get(groupPosition).get(
// childPosition));
// }
// return textView; ViewHolder viewHolder = null;
if (null == convertView) {
convertView = View.inflate(Expandablelistview2Activity.this,
R.layout.expandablelistview2_child, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) convertView
.findViewById(R.id.textView1); viewHolder.checkBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
} if (isSelectedList.get(groupPosition).get(childPosition) == 3) {
// viewHolder.checkBox
// .setBackgroundResource(R.drawable.btn_select);
// 更改CheckBox的外观
viewHolder.checkBox.setButtonDrawable(R.drawable.btn_select);
} else {
// viewHolder.checkBox
// .setBackgroundResource(R.drawable.btn_unselect);
viewHolder.checkBox.setButtonDrawable(R.drawable.btn_unselect);
}
viewHolder.textView.setText(childDataList.get(groupPosition).get(
childPosition));
return convertView;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
} private TextView createView(String content) {
TextView textView = null;
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 80);
textView = new TextView(Expandablelistview2Activity.this);
textView.setLayoutParams(layoutParams);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(80, 0, 0, 0);
textView.setText(content);
return textView;
} } private class ViewHolder {
TextView textView;
CheckBox checkBox;
ImageView imageView;
}
}
所需图片:
没有好的图标,故随便找了几张先顶一下,嘿嘿!
Checkbox的半选图标:
Checkbox的选中图标:
Checkbox的未选中图标:
备注:
是否发现缺少了一个主布局文件(expandablelistview1.xml)?它在我的前一篇中,这两篇所用都是同一个主布局文件。
简单的实现了一下,请各位多多指教,菜鸟在这儿恭候!
效果图:
Android ExpandableListView 带有Checkbox的简单应用的更多相关文章
- [转]Android中自定义checkbox样式
android中自定义checkbox的图片和大小 其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...
- Xamarin.Android之引导页的简单制作
0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...
- Android Activity的生命周期简单总结
Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...
- 【开源项目4】Android ExpandableListView
如果你对Android提供的Android ExpandableListView并不满意,一心想要实现诸如Spotify应用那般的效果,那么SlideExpandableListView绝对是你最好的 ...
- android listview 使用checkbox问题
在android中使用listview时需要了解listview加载数据的原理,为了避免listview由于列表项过多每次需要进行new造成性能低下的问题,android中的listview使用了控件 ...
- Android 设计随便说说之简单实践(合理组合)
上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...
- 怎样在Android实现桌面清理内存简单Widget小控件
怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...
- Android SQLite与ListView的简单使用
2017-04-25 初写博客有很多地方都有不足,希望各位大神给点建议. 回归主题,这次简单的给大家介绍一下Android SQLite与ListView的简单使用sqlite在上节中有介绍,所以在这 ...
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...
随机推荐
- Php 操作事务
PHP来操作数据库 关于事务操作 连接数据 mysql_connect('localhost','root','123'); 设置字符集 mysql_query('set names utf8'); ...
- JavaScript的语法规则
JavaScript的语法规则 JavaScript区分大小写 JavaScript脚本程序须嵌入在HTML文件中 JavaScript脚本程序中不能包含HTML标记代码 每行写一条脚本语句 语句末尾 ...
- spring事务到底用于service层还是dao层
Spring事务为业务逻辑进行事务管理,保证业务逻辑上数据的原子性. 事务得根据项目性质来细分:事务可以设置到三个层面(dao层.service层和web层). 第一:web层事务,这一般是针对那些安 ...
- OpenCV基本架构[OpenCV 笔记0]
最近正在系统学习OpenCV,将不定期发布笔记,主要按照毛星云的<OpenCV3编程入门>的顺序学习,会参考官方教程和文档.学习工具是Xcode+CMake,会对书中一部分内容更正,并加入 ...
- vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.
公司开发只能用英文版的VS2005 (雅蠛蝶) Tool-->Options-->Debugging->General: 把Require source files to exact ...
- 【转】C#类的分类(静态类、实例类、嵌套类、结构、简单的抽象类、简单的密封类)
静态类 -------------------------------------------------------------------------------- 静态类就是在class关键字前 ...
- L007-oldboy-mysql-dba-lesson07
L007-oldboy-mysql-dba-lesson07 [root@web01 ~]# mysqldump -uroot -ptestpassword -A >/root/mysql_ba ...
- SMB/CIFS协议解析一概述
一.SMB/CIFS协议的区别 在NetBIOS出现之后,Microsoft就使用NetBIOS实现了一个网络文件/打印服务系统,这个系统基于NetBIOS设定了一套文件共享协 议,Microsoft ...
- 使用AE进行点的坐标投影变换
private IPoint PRJtoGCS( double x, double y) { IPoint pPoint = new PointClass(); pPoint.PutCoords(x, ...
- window.print打印指定div实例代码
window.print可以打印网页,但有时候我们只希望打印特定控件或内容,怎么办呢,请看下面的例子 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. 复制代码代码如下: <h ...