MainActivity的代码
package likuo.bwie.com.day19;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String url = "http://www.zhaoapi.cn/product/getCarts";
private CheckBox checkall;
private TextView allprice;
private Button jiebutton;
MyAdapter mMyAdapter;
private ExpandableListView explistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
HashMap<String, String> map = new HashMap<>();
map.put("uid", "71");
OkhtttpUtils.getInstance().doPost(url, map, new OkhtttpUtils.OkCallback() {
@Override
public void onFailure(Exception e) {
// Toast.makeText(MainActivity.this, "没有网喇!!", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(String json) {
Person person = new Gson().fromJson(json, Person.class);
List<Person.DataBean> data = person.getData();
mMyAdapter = new MyAdapter(data);
explistview.setAdapter(mMyAdapter);
for (int i = 0; i < data.size(); i++) {
explistview.expandGroup(i);
}
mMyAdapter.setOnListChangListener(new MyAdapter.onListChangListener() {
@Override
public void onGroupCheckChange(int groupPosition) {
boolean shangJiaChecked = mMyAdapter.isShangJiaChecked(groupPosition);
mMyAdapter.isCheckedGroupisCheck(groupPosition,!shangJiaChecked);
mMyAdapter.notifyDataSetChanged();
refreshSelect();
}
@Override
public void onChildCheckChange(int groupPosition, int childPosition) {
mMyAdapter.isCheckedChildisCheck(groupPosition,childPosition);
mMyAdapter.notifyDataSetChanged();
refreshSelect();
}
@Override
public void onNumberCheckChange(int groupPosition, int childPosition, int number) {
mMyAdapter.changeShopNumber(groupPosition,childPosition,number);
mMyAdapter.notifyDataSetChanged();
refreshSelect();
}
});
}
});
}
private void initView() {
checkall = (CheckBox) findViewById(R.id.checkall);
allprice = (TextView) findViewById(R.id.allprice);
jiebutton = (Button) findViewById(R.id.jiebutton);
checkall.setOnClickListener(this);
explistview = (ExpandableListView) findViewById(R.id.explistview);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.checkall:
boolean allChecked = mMyAdapter.isAllChecked();
mMyAdapter.isCheckedAllisCheck(allChecked);
mMyAdapter.notifyDataSetChanged();
refreshSelect();
break;
}
}
private void refreshSelect(){
boolean allChecked = mMyAdapter.isAllChecked();
checkall.setChecked(allChecked);
float checkedNumberPrice = mMyAdapter.isCheckedNumberPrice();
allprice.setText("总价"+checkedNumberPrice);
int checkedNumber = mMyAdapter.isCheckedNumber();
jiebutton.setText("去结算"+checkedNumber);
}
}
adapter的代码
package likuo.bwie.com.day19;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
/**
* date:2018/11/21
* author:李阔(淡意衬优柔)
* function:
*/
public class MyAdapter extends BaseExpandableListAdapter {
List<Person.DataBean> mDataBeanList;
public MyAdapter(List<Person.DataBean> data) {
mDataBeanList = data;
}
@Override
public int getGroupCount() {
return mDataBeanList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mDataBeanList.get(groupPosition).getList().size();
}
public static class GroupViewHoder {
public CheckBox checkbox;
public TextView textview;
public GroupViewHoder(View rootView) {
this.checkbox = (CheckBox) rootView.findViewById(R.id.checkbox);
this.textview = (TextView) rootView.findViewById(R.id.textview);
}
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Person.DataBean dataBean = mDataBeanList.get(groupPosition);
GroupViewHoder groupViewHoder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item, null);
groupViewHoder = new GroupViewHoder(convertView);
convertView.setTag(groupViewHoder);
} else {
groupViewHoder = (GroupViewHoder) convertView.getTag();
}
groupViewHoder.textview.setText(dataBean.getSellerName());
//D.根据当前商家的所有商品,确定商家的CheckBox是否选中
boolean shangJiaChecked = isShangJiaChecked(groupPosition);
groupViewHoder.checkbox.setChecked(shangJiaChecked);
groupViewHoder.checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnListChangListener != null){
mOnListChangListener.onGroupCheckChange(groupPosition);
}
}
});
return convertView;
}
public static class ChildViewHoder {
public View rootView;
public CheckBox checkbox1;
public ImageView image;
public TextView textview1;
public TextView textview2;
public MyCustom Mycustom;
public ChildViewHoder(View rootView) {
this.rootView = rootView;
this.checkbox1 = (CheckBox) rootView.findViewById(R.id.checkbox1);
this.image = (ImageView) rootView.findViewById(R.id.image);
this.textview1 = (TextView) rootView.findViewById(R.id.textview1);
this.textview2 = (TextView) rootView.findViewById(R.id.textview2);
this.Mycustom = (MyCustom) rootView.findViewById(R.id.Mycustom);
}
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHoder childViewHoder;
Person.DataBean dataBean = mDataBeanList.get(groupPosition);
List<Person.DataBean.ListBean> list = dataBean.getList();
Person.DataBean.ListBean listBean = list.get(childPosition);
String images = listBean.getImages();
String[] split = images.split("!");
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_i, null);
childViewHoder = new ChildViewHoder(convertView);
convertView.setTag(childViewHoder);
} else {
childViewHoder = (ChildViewHoder) convertView.getTag();
}
childViewHoder.textview1.setText(listBean.getTitle());
childViewHoder.textview2.setText("¥"+listBean.getPrice()+"");
childViewHoder.checkbox1.setChecked(listBean.getSelected() == 1);
Glide.with(parent.getContext()).load(split[0]).into(childViewHoder.image);
childViewHoder.Mycustom.setNumber(listBean.getNum());
childViewHoder.checkbox1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnListChangListener != null){
mOnListChangListener.onChildCheckChange(groupPosition,childPosition);
}
}
});
childViewHoder.Mycustom.setOnNumberChangeListener(new MyCustom.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
if (mOnListChangListener != null){
mOnListChangListener.onNumberCheckChange(groupPosition,childPosition,num);
}
}
});
return convertView;
}
//商家的商品是否选中
public boolean isShangJiaChecked(int groupPosition){
Person.DataBean dataBean = mDataBeanList.get(groupPosition);
List<Person.DataBean.ListBean> list = dataBean.getList();
for (Person.DataBean.ListBean listbean : list) {
if(listbean.getSelected() == 0){
return false;
}
}
return true;
}
//全选是否选中
public boolean isAllChecked(){
for (int i = 0; i < mDataBeanList.size(); i++) {
Person.DataBean dataBean = mDataBeanList.get(i);
List<Person.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
Person.DataBean.ListBean listBean = list.get(j);
if (listBean.getSelected()== 0){
return false;
}
}
}
return true;
}
//拿到选中的商品的数量
public int isCheckedNumber(){
int NumberLK = 0;
for (int i = 0; i < mDataBeanList.size(); i++) {
Person.DataBean dataBean = mDataBeanList.get(i);
List<Person.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
Person.DataBean.ListBean listBean = list.get(j);
if (listBean.getSelected() == 1){
int num = listBean.getNum();
NumberLK += num;
}
}
}
return NumberLK;
}
//拿到选中商品的价格
public float isCheckedNumberPrice(){
float PriceLK = 0;
for (int i = 0; i < mDataBeanList.size(); i++) {
Person.DataBean dataBean = mDataBeanList.get(i);
List<Person.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
Person.DataBean.ListBean listBean = list.get(j);
if (listBean.getSelected() == 1){
float price = listBean.getPrice();
int num = listBean.getNum();
PriceLK +=price*num;
}
}
}
return PriceLK;
}
//点击商家的全选框如果有不选的就是false然后就全选,如果全是选中的就让他全不选
public void isCheckedGroupisCheck(int groupPosition,boolean isSelected){
Person.DataBean dataBean = mDataBeanList.get(groupPosition);
List<Person.DataBean.ListBean> list = dataBean.getList();
for (int i = 0; i < list.size(); i++) {
Person.DataBean.ListBean listBean = list.get(i);
listBean.setSelected(isSelected ? 1 : 0);
}
}
//当商家的子条目点击选中时,更新
public void isCheckedChildisCheck(int groupPosition,int childPosition){
Person.DataBean dataBean = mDataBeanList.get(groupPosition);
List<Person.DataBean.ListBean> list = dataBean.getList();
Person.DataBean.ListBean listBean = list.get(childPosition);
listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
}
//点击全选框如果有不选的就是false然后就全选,如果全是选中的就让他全不选
public void isCheckedAllisCheck(boolean selected){
for (int i = 0; i < mDataBeanList.size(); i++) {
Person.DataBean dataBean = mDataBeanList.get(i);
List<Person.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
Person.DataBean.ListBean listBean = list.get(j);
listBean.setSelected(selected ? 0 : 1 );
}
}
}
//点击加减器改变商品数量 参数1.定位哪家 2.定位哪个商品 3.具体数量
public void changeShopNumber(int groupPosition,int childPosition,int number){
Person.DataBean dataBean = mDataBeanList.get(groupPosition);
List<Person.DataBean.ListBean> list = dataBean.getList();
Person.DataBean.ListBean listBean = list.get(childPosition);
listBean.setNum(number);
}
public interface onListChangListener{
/**
* 当商家条目点击时回调
*/
void onGroupCheckChange(int groupPosition);
/**
* 当商家子条目点击时回调
*/
void onChildCheckChange(int groupPosition,int childPosition);
/**
* 当商家子条目的加减号点击时回调
*/
void onNumberCheckChange(int groupPosition,int childPosition,int number);
}
onListChangListener mOnListChangListener;
public void setOnListChangListener(onListChangListener onListChangListener) {
mOnListChangListener = onListChangListener;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
}
自定义控件商品数量加减号
package likuo.bwie.com.day19;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* date:2018/11/20
* author:李阔(淡意衬优柔)
* function:
*/
public class MyCustom extends LinearLayout implements View.OnClickListener {
private TextView sub_tv;
private TextView add_tv;
private TextView product_number_tv;
int number = 1;
public MyCustom(Context context) {
this(context,null);
}
public MyCustom(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyCustom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = inflate(context,R.layout.kongjian,this);
sub_tv = view.findViewById(R.id.sub_tv);
product_number_tv = view.findViewById(R.id.product_number_tv);
add_tv = view.findViewById(R.id.add_tv);
add_tv.setOnClickListener(this);
sub_tv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sub_tv:
if (number > 1) {
--number;
product_number_tv.setText(number + "");
if (onNumberChangeListener != null) {
onNumberChangeListener.onNumberChange(number);
}
} else {
Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_tv:
++number;
product_number_tv.setText(number + "");
if (onNumberChangeListener != null) {
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
product_number_tv.setText(number + "");
}
OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
interface OnNumberChangeListener {
void onNumberChange(int num);
}
}
OKHTTP的封装
package likuo.bwie.com.day19;
import android.os.Handler;
import android.os.Looper;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkhtttpUtils {
private static OkhtttpUtils mOkhtttpUtils;
private OkHttpClient mOkHttpClien;
private final Handler mHandler;
private OkhtttpUtils() {
//创建一个主线程的handler
mHandler = new Handler(Looper.getMainLooper());
mOkHttpClien = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.build();
}
//单例模式
public static OkhtttpUtils getInstance() {
if (mOkhtttpUtils == null) {
synchronized (OkhtttpUtils.class) {
if (mOkhtttpUtils == null) {
return mOkhtttpUtils = new OkhtttpUtils();
}
}
}
return mOkhtttpUtils;
}
public interface OkCallback {
void onFailure(Exception e);
void onResponse(String json);
}
public void doPost(String url, Map<String, String> map, final OkCallback okCallback) {
//创建FormBody的对象,把表单添加到formBody中
FormBody.Builder builder = new FormBody.Builder();
if (map != null) {
for (String key : map.keySet()) {
builder.add(key, map.get(key));
}
}
FormBody formBody = builder.build();
//创建Request对象
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
//创建Call对象
final Call call = mOkHttpClien.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (okCallback != null) {
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
okCallback.onFailure(e);
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
try {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
if (okCallback != null) {
okCallback.onResponse(json);
return;
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
if (okCallback != null) {
okCallback.onFailure(new Exception("网络异常"));
}
}
});
}
//封装doGet的网络请求
public void doGet(String url, final OkCallback okCallback) {
Request request = new Request.Builder()
.get()
.url(url)
.build();
final Call call = mOkHttpClien.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (okCallback != null) {
//切换到主线程
mHandler.post(new Runnable() {
@Override
public void run() {
okCallback.onFailure(e);
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
try {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
if (okCallback != null) {
okCallback.onResponse(json);
return;
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
main的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ExpandableListView
android:layout_weight="10"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/explistview"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="#eeeeee"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/checkall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" />
<TextView
android:id="@+id/allprice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="20dp"
android:text="合计:¥0.00" />
<Button
android:id="@+id/jiebutton"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="去结算(0)" />
</LinearLayout>
</LinearLayout>
父条目的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<CheckBox
android:layout_marginLeft="50dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox"
android:focusable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"/>
</LinearLayout>
父条目的子条目的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_marginLeft="25dp"
android:layout_marginTop="27dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:focusable="false"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/image"/>
<LinearLayout
android:layout_width="250dp"
android:layout_height="100dp"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:text="卡多少管理会计是东升"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:id="@+id/textview2"
android:text="¥888"
/>
</LinearLayout>
<likuo.bwie.com.day19.MyCustom
android:layout_width="80dp"
android:layout_height="60dp"
android:id="@+id/Mycustom"/>
</LinearLayout>
自定义控件的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="2dp"
android:layout_marginLeft="10dp"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#99000000"
android:gravity="center_vertical">
<TextView
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/sub_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
<TextView
android:text="1"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/product_number_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
/>
<TextView
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/add_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="+"
android:textSize="16sp" />
</LinearLayout>
bean就不上传;额