效果图
清单文件: ( 入口是OtherActivity )
<uses-permission android:name="android.permission.INTERNET" />
依赖包:
compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.github.orangegangsters:swipy:1.1.0@aar' compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.9.0' compile 'com.google.code.gson:gson:2.8.2' compile 'com.squareup.picasso:picasso:2.5.1' compile 'com.squareup.okio:okio:1.5.0' compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
okhttp3Util:
public class OkHttp3Utils {
/**
* 懒汉 安全 加同步
* 私有的静态成员变量 只声明不创建
* 私有的构造方法
* 提供返回实例的静态方法
*/
private static OkHttpClient okHttpClient = null;
private OkHttp3Utils() {
}
public static OkHttpClient getInstance() {
if (okHttpClient == null) {
//加同步安全
synchronized (OkHttp3Utils.class) {
if (okHttpClient == null) {
//判空 为空创建实例
// okHttpClient = new OkHttpClient();
/**
* 和OkHttp2.x有区别的是不能通过OkHttpClient直接设置超时时间和缓存了,而是通过OkHttpClient.Builder来设置,
* 通过builder配置好OkHttpClient后用builder.build()来返回OkHttpClient,
* 所以我们通常不会调用new OkHttpClient()来得到OkHttpClient,而是通过builder.build():
*/
// File sdcache = getExternalCacheDir();
File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
int cacheSize = 10 * 1024 * 1024;
okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).cache(new Cache(sdcache.getAbsoluteFile(), cacheSize)).build();
}
}
}
return okHttpClient;
}
/**
* get请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doGet(String url, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建Request
Request request = new Request.Builder().url(url).build();
//得到Call对象
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
/**
* post请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doPost(String url, Map<String, String> params, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历集合
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
//创建Request
Request request = new Request.Builder().url(url).post(builder.build()).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
* post请求上传文件
* 参数1 url
* 参数2 回调Callback
*/
public static void uploadPic(String url, File file, String fileName) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建RequestBody 封装file参数
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
//创建RequestBody 设置类型等
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, fileBody).build();
//创建Request
Request request = new Request.Builder().url(url).post(requestBody).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//上传成功回调 目前不需要处理
}
});
}
/**
* Post请求发送JSON数据
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
public static void doPostJson(String url, String jsonParams, Callback callback) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}
/**
* 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装
* 参数一:请求Url
* 参数二:保存文件的路径名
* 参数三:保存文件的文件名
*/
public static void download(final MainActivity context, final String url, final String saveDir) {
Request request = new Request.Builder().url(url).build();
Call call = getInstance().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("xxx", e.toString());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
//apk保存路径
final String fileDir = isExistDir(saveDir);
//文件
File file = new File(fileDir, getNameFromUrl(url));
context.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show();
}
});
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
//apk下载完成后 调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();
}
}
});
}
/**
* @param saveDir
* @return
* @throws IOException 判断下载目录是否存在
*/
public static String isExistDir(String saveDir) throws IOException {
// 下载位置
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
Log.e("savePath", savePath);
return savePath;
}
return null;
}
/**
* @param url
* @return 从下载连接中解析出文件名
*/
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
}
other.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.audrey.projectshoppingcart.activity.OtherActivity">
<RelativeLayout
android:id="@+id/rl_head"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cecece">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="购物车"
android:textColor="@color/colorAccent"
android:textSize="30sp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/rl_head"
android:layout_marginBottom="51dp">
<com.audrey.projectshoppingcart.view.CustomExpandableListView
android:id="@+id/celv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:button="@drawable/select_checkbox"
android:focusable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkbox2"
android:gravity="center_vertical"
android:text="全选"
android:textSize="20sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="合计 :" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:text="0"
android:textColor="@android:color/holo_red_light" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:padding="10dp"
android:text="结算(0)"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
OtherActivity代码:
public class OtherActivity extends AppCompatActivity implements View.OnClickListener {
private CustomExpandableListView celv;
private CheckBox checkBox;
private TextView tv_num;
private TextView tv_price;
private PhonesInfo phonesInfo;
private PhoneAdapter adapter;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
String str = (String) msg.obj;
Log.i("xxx", str.toString());
Gson gson = new Gson();
phonesInfo = gson.fromJson(str, PhonesInfo.class);
//适配配
adapter = new PhoneAdapter();
celv.setAdapter(adapter);
int count = celv.getCount();
for (int i = 0; i < count; i++) {
//展开
celv.expandGroup(i);
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
//获取数据
getData();
//找控件
celv = (CustomExpandableListView) findViewById(R.id.celv);
checkBox = (CheckBox) findViewById(R.id.checkbox2);
tv_num = (TextView) findViewById(R.id.tv_num);
tv_price = (TextView) findViewById(R.id.tv_price);
//去掉箭头
celv.setGroupIndicator(null);
//CheckBox点击事件
checkBox.setOnClickListener(this);
}
//获取数据
private void getData() {
String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=evaluation";
OkHttp3Utils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.i("xxx", string.toString());
Message message = handler.obtainMessage(1, string);
message.sendToTarget();
}
});
}
//全选的监听
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
List<PhonesInfo.DataInfo> data = phonesInfo.getData();
for (int i = 0; i < data.size(); i++) {
Log.i("xxx", data.get(i).isAllCheck() + "");
PhonesInfo.DataInfo groupDatas = phonesInfo.getData().get(i);
groupDatas.setAllCheck(true);
List<PhonesInfo.DataInfo.DatasInfo> datas = data.get(i).getDatas();
for (int j = 0; j < datas.size(); j++) {
Log.i("xxx", datas.get(j).isItemCheck() + "");
List<PhonesInfo.DataInfo.DatasInfo> childDatas = groupDatas.getDatas();
for (PhonesInfo.DataInfo.DatasInfo childData : childDatas) {
childData.setItemCheck(true);
}
}
}
//刷新界面
notifyCheckAdapter();
} else {
List<PhonesInfo.DataInfo> data = phonesInfo.getData();
for (int i = 0; i < data.size(); i++) {
Log.i("xxx", data.get(i).isAllCheck() + "");
PhonesInfo.DataInfo groupDatas = phonesInfo.getData().get(i);
groupDatas.setAllCheck(false);
List<PhonesInfo.DataInfo.DatasInfo> datas = data.get(i).getDatas();
for (int j = 0; j < datas.size(); j++) {
Log.i("xxx", datas.get(j).isItemCheck() + "");
List<PhonesInfo.DataInfo.DatasInfo> childDatas = groupDatas.getDatas();
for (PhonesInfo.DataInfo.DatasInfo childData : childDatas) {
childData.setItemCheck(false);
}
}
}
//刷新界面
notifyCheckAdapter();
}
}
//适配器
private class PhoneAdapter implements ExpandableListAdapter {
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getGroupCount() {
return phonesInfo.getData().size();
}
@Override
public int getChildrenCount(int groupPosition) {
return phonesInfo.getData().get(groupPosition).getDatas().size();
}
@Override
public Object getGroup(int groupPosition) {
return phonesInfo.getData().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return phonesInfo.getData().get(groupPosition).getDatas().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(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = View.inflate(OtherActivity.this, R.layout.item_parent_market, null);
CheckBox cb_parent = (CheckBox) view.findViewById(R.id.cb_parent);
TextView tv_number = (TextView) view.findViewById(R.id.tv_number);
tv_number.setText(phonesInfo.getData().get(groupPosition).getTitle());
if (phonesInfo.getData().get(groupPosition).isAllCheck()) {
cb_parent.setChecked(true);
} else {
cb_parent.setChecked(false);
}
//一级监听
cb_parent.setOnClickListener(new OnGroupClickListener(groupPosition, cb_parent));
return view;
}
//二级
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = View.inflate(OtherActivity.this, R.layout.item_child_market, null);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
TextView tv_pri = (TextView) view.findViewById(R.id.tv_pri);
CheckBox cb_child = (CheckBox) view.findViewById(R.id.cb_child);
tv_pri.setText(phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).getPrice() + "");
tv_content.setText(phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).getType_name());
tv_time.setText(phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).getAdd_time());
if (phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).isItemCheck()) {
cb_child.setChecked(true);
} else {
cb_child.setChecked(false);
}
cb_child.setOnClickListener(new OnChildCheckListener(groupPosition, childPosition, cb_child));
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
}
//一级监听
private class OnGroupClickListener implements View.OnClickListener {
int groupPosition;
CheckBox cb_parent;
public OnGroupClickListener(int groupPosition, CheckBox cb_parent) {
this.cb_parent = cb_parent;
this.groupPosition = groupPosition;
}
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//一级全选
setCheck(true);
} else {
//取消全选
setCheck(false);
checkBox.setChecked(false);
}
notifyCheckAdapter();
}
public void setCheck(boolean checkFlag) {
PhonesInfo.DataInfo groupDatas = phonesInfo.getData().get(groupPosition);
List<PhonesInfo.DataInfo> data = phonesInfo.getData();
//一级状态
groupDatas.setAllCheck(checkFlag);
//全选状态判断
int num = 0;
for (int i = 0; i < data.size(); i++) {
boolean allCheck = data.get(i).isAllCheck();
if (!allCheck) {
num++;
}
}
if (num == 0) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
//二级状态
List<PhonesInfo.DataInfo.DatasInfo> childDatas = groupDatas.getDatas();
for (PhonesInfo.DataInfo.DatasInfo childData : childDatas) {
//二级状态
childData.setItemCheck(checkFlag);
}
}
}
//二级监听
private class OnChildCheckListener implements View.OnClickListener {
int groupPosition;
int childPosition;
CheckBox cb_child;
public OnChildCheckListener(int groupPosition, int childPosition, CheckBox cb_child) {
this.cb_child = cb_child;
this.groupPosition = groupPosition;
this.childPosition = childPosition;
}
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//子选中
phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).setItemCheck(true);
} else {
//子未选中
phonesInfo.getData().get(groupPosition).getDatas().get(childPosition).setItemCheck(false);
}
//二级联动一级状态
setParentCheckFlag();
//检测状态 二级联动全选
int num = 0;
for (int i = 0; i < phonesInfo.getData().size(); i++) {
boolean allCheck = phonesInfo.getData().get(i).isAllCheck();
if (!allCheck) {
num++;
}
}
if (num == 0) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
}
//二级联动一级状态
private void setParentCheckFlag() {
PhonesInfo.DataInfo dataInfo = phonesInfo.getData().get(groupPosition);
List<PhonesInfo.DataInfo.DatasInfo> datasInfos = dataInfo.getDatas();
for (int i = 0; i < datasInfos.size(); i++) {
if (!datasInfos.get(i).isItemCheck()) {
//子未选中 父取消选中
dataInfo.setAllCheck(false);
notifyCheckAdapter();
return;
}
if (i == datasInfos.size() - 1) {
//子选中 父选中
dataInfo.setAllCheck(true);
notifyCheckAdapter();
return;
}
}
// 没出现全选或者取消全选的时候执行的
sum();
}
}
//统计数量和价格
private void sum() {
int num = 0;
int price = 0;
List<PhonesInfo.DataInfo> data = phonesInfo.getData();
for (PhonesInfo.DataInfo parentData : data) {
for (PhonesInfo.DataInfo.DatasInfo child : parentData.getDatas()) {
if (child.isItemCheck()) {
num++;
price += child.getPrice();
}
}
}
tv_num.setText("结算(" + num + ")");
tv_price.setText("¥" + price);
}
//刷新适配器界面
private void notifyCheckAdapter() {
sum();
celv.setAdapter(adapter);
int count = celv.getCount();
for (int i = 0; i < count; i++) {
celv.expandGroup(i);
}
}
}
mian.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.audrey.projectshoppingcart.activity.MainActivity">
<com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout
android:id="@+id/srl"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"/>
<!--android:button="@drawable/select_checkbox"-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="6dp"
android:layout_toRightOf="@+id/checkbox"
android:text="全选"
android:textSize="20sp" />
</RelativeLayout>
</RelativeLayout>
MainActivity代码:
MainActivity的适配器:public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String url = "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=one"; private RecyclerView rv; private SwipyRefreshLayout srl; private CheckBox checkbox; private MyAdapter adapter; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 0: String json = (String) msg.obj; try { JSONObject jsonObject = new JSONObject(json); JSONArray data = jsonObject.getJSONArray("data"); //设置适配器 adapter = new MyAdapter(MainActivity.this, data, new MyAdapter.OnCheckListener() { @Override public void onCheck(boolean isCheck) { //当所有条目都选中时 全选将全中 checkbox.setChecked(isCheck); } }); rv.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { rv = (RecyclerView) findViewById(R.id.rv); srl = (SwipyRefreshLayout) findViewById(R.id.srl); checkbox = (CheckBox) findViewById(R.id.checkbox); checkbox.setOnClickListener(this); } @Override public void onClick(View v) { //设置布局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); rv.setLayoutManager(linearLayoutManager); //设置支持上拉刷新和下拉加载 srl.setDirection(SwipyRefreshLayoutDirection.BOTH); //设置刷新时动画的颜色,可以设置4个 srl.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); srl.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() { @Override public void onRefresh(SwipyRefreshLayoutDirection direction) { //默认不选中 checkbox.setChecked(false); getData(); } }); } private void getData() { } }
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private Context context; private JSONArray data; private Map<String, Boolean> map = new HashMap<>(); public MyAdapter(Context context, JSONArray data, OnCheckListener listener) { this.context = context; this.data = data; setCheckData(false); this.listener = listener; } //全选控制适配器item private void setCheckData(boolean checkFlag) { map.clear(); for (int i = 0; i < data.length(); i++) { try { JSONObject jsonObject = data.getJSONObject(i); String id = jsonObject.getString("id"); map.put(id, checkFlag); } catch (JSONException e) { e.printStackTrace(); } } } public void notifCheckData(boolean checkFlag){ setCheckData(checkFlag); notifyDataSetChanged(); } //创建view设置给ViewHolder @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false); return new MyViewHolder(view); } //绑定数据 @Override public void onBindViewHolder(MyViewHolder holder, int position) { try { JSONObject jsonObject = data.getJSONObject(position); String image_url = jsonObject.optString("image_url"); String title = jsonObject.getString("title"); final String id = jsonObject.getString("id"); holder.tv_title.setText(title); Picasso.with(context).load(image_url).placeholder(R.mipmap.ic_launcher).into(holder.iv); holder.cb.setChecked(map.get(id)); holder.cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //根据点击选中状态 boolean checked = ((CheckBox) view).isChecked(); map.put(id, checked); boolean isChecked = true; //遍历集合 for (String key : map.keySet()) { Boolean value = map.get(key); if (!value) { isChecked = false; if (listener != null) { //取消全选 listener.onCheck(isChecked); } return; } } if (isChecked) { //全选 if (listener != null) { listener.onCheck(isChecked); } } } }); } catch (JSONException e) { e.printStackTrace(); } } //数据长度 @Override public int getItemCount() { return data.length(); } public class MyViewHolder extends RecyclerView.ViewHolder { private ImageView iv; private TextView tv_title; private CheckBox cb; public MyViewHolder(View itemView) { super(itemView); iv = (ImageView) itemView.findViewById(R.id.iv); tv_title = (TextView) itemView.findViewById(R.id.tv_title); cb = (CheckBox) itemView.findViewById(R.id.cb); } } private OnCheckListener listener; public interface OnCheckListener { void onCheck(boolean isCheck); } public void setOnCheckListener(OnCheckListener listener) { this.listener = listener; } }
main.xml文件里的CustomExpandableListView class文件public class CustomExpandableListView extends ExpandableListView { public CustomExpandableListView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
bean包:
public class PhonesInfo { public String flag; public String code; public List<DataInfo> data; public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataInfo> getData() { return data; } public void setData(List<DataInfo> data) { this.data = data; } public static class DataInfo { public String title; public List<DatasInfo> datas; public boolean allCheck = false; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<DatasInfo> getDatas() { return datas; } public void setDatas(List<DatasInfo> datas) { this.datas = datas; } public boolean isAllCheck() { return allCheck; } public void setAllCheck(boolean allCheck) { this.allCheck = allCheck; } public static class DatasInfo { public int price; public String type_name; public String msg; public String add_time; public boolean itemCheck = false; public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getType_name() { return type_name; } public void setType_name(String type_name) { this.type_name = type_name; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getAdd_time() { return add_time; } public void setAdd_time(String add_time) { this.add_time = add_time; } public boolean isItemCheck() { return itemCheck; } public void setItemCheck(boolean itemCheck) { this.itemCheck = itemCheck; } } } }