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的简单应用的更多相关文章

  1. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  2. Xamarin.Android之引导页的简单制作

    0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...

  3. Android Activity的生命周期简单总结

    Android Activity的生命周期简单总结 这里的内容参考官方的文档,这篇文章的目的不是去总结Activity是如何启动,如何创造,以及暂停和销毁的,而是从实际开发中分析在Activity各个 ...

  4. 【开源项目4】Android ExpandableListView

    如果你对Android提供的Android ExpandableListView并不满意,一心想要实现诸如Spotify应用那般的效果,那么SlideExpandableListView绝对是你最好的 ...

  5. android listview 使用checkbox问题

    在android中使用listview时需要了解listview加载数据的原理,为了避免listview由于列表项过多每次需要进行new造成性能低下的问题,android中的listview使用了控件 ...

  6. Android 设计随便说说之简单实践(合理组合)

    上一篇(Android 设计随便说说之简单实践(模块划分))例举了应用商店设计来说明怎么做模块划分.模块划分主要依赖于第一是业务需求,具体是怎么样的业务.应用商店则包括两个业务,就是向用户展示appl ...

  7. 怎样在Android实现桌面清理内存简单Widget小控件

    怎样在Android实现桌面清理内存简单Widget小控件 我们常常会看到类似于360.金山手机卫士一类的软件会带一个widget小控件,显示在桌面上,上面会显示现有内存大小,然后会带一个按键功能来一 ...

  8. Android SQLite与ListView的简单使用

    2017-04-25 初写博客有很多地方都有不足,希望各位大神给点建议. 回归主题,这次简单的给大家介绍一下Android SQLite与ListView的简单使用sqlite在上节中有介绍,所以在这 ...

  9. Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址

    Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...

随机推荐

  1. angular的post请求,SpringMVC后台接收不到参数值的解决方案

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  2. AngularJS开发相关配置

    安装步骤: 1. Node.js,下载地址:https://nodejs.org/en/ 2. Git 下载地址:https://git-scm.com/download/ 3. Python (需为 ...

  3. 在java中HttpServletResponse响应中文出现乱码。

    以字符串的形式输出. 1.response.getWriter().write("您好中国hello"); 如果这样输出的话.则浏览器结果为: 2.加上代码 response.se ...

  4. 洛谷 P1508 Likecloud-吃、吃、吃

    P1508 Likecloud-吃.吃.吃 题目提供者JosephZheng 标签 动态规划 难度 普及/提高- 题目背景 问世间,青春期为何物? 答曰:"甲亢,甲亢,再甲亢:挨饿,挨饿,再 ...

  5. strcpy、strncpy、strlcpy的区别

  6. iOS 简单总结:description方法\NSLog函数

    1.description方法是NSObject自带的方法,包括类方法和对象方法 + (NSString *)description; // 默认返回 类名 - (NSString *)descrip ...

  7. SQL技巧之行列转换

    比如:id     姓名      状态  1      刘德华    12      刘德华    23      周华健    04      吴彦祖    1 在access中,用一条sql查询 ...

  8. ASP.NET文件上传的三种基本方法

    ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录. <form i ...

  9. Ubuntu的默认root密码

    Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo passwd,然后输入当前用户的密码,enter,终端会提示我们输入新的密码并确认,此时的 ...

  10. iOS编程——经过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版)

    iOS编程——通过UUID和KeyChain来代替Mac地址实现iOS设备的唯一标示(OC版) 很多的应用都需要用到手机的唯一标示,而且要求这个唯一标示不能因为应用app的卸载或者改变而变化. 在iO ...