一、导入依赖
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile files('libs/gson-2.2.4.jar')
二、添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
三、MainActivity的布局
<?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"
android:orientation="vertical"
tools:context="com.bw.lenovo.goodscart.MainActivity">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="购物车"
android:textSize="25sp"/>
<View
android:background="#999999"
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/expan_list_view"
android:groupIndicator="@null"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xuan"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
<TextView
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sum"
android:text="合计:¥0"
android:textSize="25sp"/>
<Button
android:id="@+id/toJisuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
android:text="去结算"/>
</LinearLayout>
</LinearLayout>
四、自定义控件(加减按钮)的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:textSize="25sp"
/>
<EditText
android:id="@+id/count"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:text="0"
android:focusable="false"
/>
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:text="+"
android:textSize="25sp"/>
</LinearLayout>
五、ExpandableListView的父视图布局
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parent_cb"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商家"
android:textSize="20sp"
android:id="@+id/parent_title"/>
</LinearLayout>
六、ExpandableListView的子视图布局
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/child_cb"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/child_icon"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品名称"
android:layout_weight="1"
android:id="@+id/child_title"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/child_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格:0"
android:textColor="@android:color/holo_red_light"/>
<com.bw.lenovo.goodscart.view.AddSubView
android:id="@+id/add_sub_view"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_marginLeft="40dp"></com.bw.lenovo.goodscart.view.AddSubView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
七、http包下的 httpConfig (接口路径)
package com.bw.lenovo.goodscart.http;
public class HttpConfig {
public static String url = "https://www.zhaoapi.cn/product/getCarts?source=android&uid=71";
}
八、http包下的 OkhttpUtils(ok封装网络框架)
package com.bw.lenovo.lianxi_goodscart.Http;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OKHttpUtils {
private static final String TAG = "OKHttpUtils===========";
private static OKHttpUtils okHttpUtils = null;
private MyHandler myhandler = new MyHandler();
private OKLoadLinsener okLoadLinsener;
public static OKHttpUtils getInstance() {
if (okHttpUtils == null) {
okHttpUtils = new OKHttpUtils();
}
return okHttpUtils;
}
public void okGet(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure() 失败=====");
Message message = myhandler.obtainMessage();
message.what = 0;
message.obj = e.getMessage();
myhandler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "onResponse() 成功=====");
Message message = myhandler.obtainMessage();
message.what = 1;
message.obj = response.body().string();
myhandler.sendMessage(message);
}
});
}
//将输入发送到主线程,处理线程问题
public class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//获取消息的识别码 返回int类型的值
int wh = msg.what;
switch (wh) {
//失败
case 0:
String error = (String) msg.obj;
okLoadLinsener.LoadError(error);
break;
//成功
case 1:
String json = (String) msg.obj;
okLoadLinsener.LoadSuccess(json);
break;
}
}
}
//创建ok加载时的监听 (一般加载时就两种状态)失败 +成功
public interface OKLoadLinsener {
//成功
void LoadSuccess(String json);
//失败
void LoadError(String error);
}
// 设置提供一个供外部访问的方法
public void setOnLoadListener(OKLoadLinsener okLoadLinsener) {
this.okLoadLinsener = okLoadLinsener;
}
}
九、http包下的GetCartListLinsener
package com.bw.lenovo.goodscart.http;
import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;
/**
* 获取购物车的监听
*/
public interface GetCartListListener {
void getCartSuccess(List<CartListBean.DataBean> list);
// 失败
void getCartError(String error);
}
十、model包下需要写一个Bean类==解析接口数据(一键解析)
十一、model包下写ICartModel
package com.bw.lenovo.goodscart.model;
import com.bw.lenovo.goodscart.http.GetCartListListener;
/**
* 去请求购物车数据
*/
public interface ICartModel {
void getCartList(String url, GetCartListListener getCartListListener);
}
十二、model包下写 CartModelmpI
package com.bw.lenovo.goodscart.model;
import android.util.Log;
import com.bw.lenovo.goodscart.http.GetCartListListener;
import com.bw.lenovo.goodscart.http.HttpConfig;
import com.bw.lenovo.goodscart.http.OKHttpUtils;
import com.google.gson.Gson;
import java.util.List;
import static android.content.ContentValues.TAG;
public class CartModelImpl implements ICartModel {
private static final String TAG = "CartModelImpl======";
@Override
public void getCartList(String url, final GetCartListListener getCartListListener) {
OKHttpUtils okHttpUtils = OKHttpUtils.getInstance();
okHttpUtils.okGet(url);
okHttpUtils.setOnLoadListener(new OKHttpUtils.OKLoadListener() {
@Override
public void loadSuccess(String json) {
Log.d(TAG, "成功" + json);
//网络请求成功
Gson gson = new Gson();
CartListBean cartListBean = gson.fromJson(json, CartListBean.class);
List<CartListBean.DataBean> list = cartListBean.getData();
// 对象
getCartListListener.getCartSuccess(list);
}
@Override
public void loadError(String error) {
//网络请求失败
getCartListListener.getCartError(error);
}
});
}
}
十三、view包下的IView
package com.bw.lenovo.goodscart.view;
import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;
public interface IView {
//展示
void showCartList(List<CartListBean.DataBean> list);
//显示总价
void showCount(String count);
}
十四、IView包下的自定义控件(加减控件)
package com.bw.lenovo.goodscart.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bw.lenovo.goodscart.R;
public class AddSubView extends LinearLayout implements View.OnClickListener {
private static final String TAG = "AddSubView=============";
private TextView sub;
private EditText count;
private TextView add;
private AddClickListener addClickListener;
private SubClickListener subClickListener;
public AddSubView(Context context) {
this(context, null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.add_sub_layout, this);
//获取控件
sub = view.findViewById(R.id.sub);
count = view.findViewById(R.id.count);
add = view.findViewById(R.id.add);
sub.setOnClickListener(this);
add.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View view) {
switch (view.getId()) {
// 当id为减时
case R.id.sub:
// 获取当时输入的内容
String c = count.getText().toString();
// 装换成int类型
int i = Integer.parseInt(c);
// 判断i小于0的时候
if (i <= 0) {
// 停止
return;
}
count.setText(--i + "");
subClickListener.onSubClick(view, i);
Log.d(TAG, "减=======" + i);
break;
case R.id.add:
String c1 = count.getText().toString();
int i1 = Integer.parseInt(c1);
count.setText(++i1 + "");
addClickListener.onAddClick(view, i1);
Log.d(TAG, "onClick() returned:" + "加=======");
break;
}
}
//获取内容的方法
public int getCount() {
//返回输入的内容转换成int类型的去掉空格
return Integer.parseInt(count.getText().toString().trim());
}
public void setCount(int s) {
count.setText(s + "");
}
//定义一个可被外部调用的接口(加)
public interface AddClickListener {
void onAddClick(View view, int count);
}
// 定义一个可被外部调用的接口(减)
public interface SubClickListener {
void onSubClick(View view, int count);
}
//加的点击事件
public void setOnAddClickListener(AddClickListener addClickListener) {
this.addClickListener = addClickListener;
}
// 减的点击事件
public void setOnSubClickListener(SubClickListener subClickListener) {
this.subClickListener = subClickListener;
}
}
十五、presenter包下的IPresenter
package com.bw.lenovo.goodscart.presenter;
import com.bw.lenovo.goodscart.model.ICartModel;
import com.bw.lenovo.goodscart.view.IView;
public interface IPresenter {
// 展示数据
void showCartList(ICartModel iCartModel, IView iView);
}
十六、presenter包下的presenterImpI
package com.bw.lenovo.goodscart.presenter;
import android.net.IpPrefix;
import com.bw.lenovo.goodscart.http.GetCartListListener;
import com.bw.lenovo.goodscart.http.HttpConfig;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.model.ICartModel;
import com.bw.lenovo.goodscart.view.IView;
import java.util.List;
public class PresenterImpl implements IPresenter{
@Override
public void showCartList(ICartModel iCartModel, final IView iView) {
// 具体的操作
iCartModel.getCartList(HttpConfig.url, new GetCartListListener() {
@Override
public void getCartSuccess(List<CartListBean.DataBean> list) {
// 传给view
iView.showCartList(list);
}
@Override
public void getCartError(String error) {
}
});
}
}
十七、view包下的 ExpandableListView的适配器
package com.bw.lenovo.goodscart.view;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.NoAnimation;
import com.bw.lenovo.goodscart.R;
import com.bw.lenovo.goodscart.Utils.SumUtils;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.presenter.PresenterImpl;
import java.util.List;
public class MyExpanableAdapter extends BaseExpandableListAdapter {
private final Context context;
private static final String TAG = "MyExpanableAdapter----";
private final List<CartListBean.DataBean> list;
private final IView iView;
public MyExpanableAdapter(Context context, List<CartListBean.DataBean> list, IView iView) {
this.context = context;
this.list = list;
this.iView = iView;
}
// 获取父分组的数量
@Override
public int getGroupCount() {
return list.size();
}
// 获取子分组的数量
@Override
public int getChildrenCount(int i) {
return list.get(i).getList().size();
}
//获取父分组的对象
@Override
public Object getGroup(int i) {
return list.get(i);
}
//获取子分组的对象
@Override
public Object getChild(int i, int i1) {
return list.get(i).getList().get(i1);
}
//获取父分组的id
@Override
public long getGroupId(int i) {
return i;
}
//获取子分组的id
@Override
public long getChildId(int i, int i1) {
return i1;
}
//保持平衡的意思
@Override
public boolean hasStableIds() {
return false;
}
// 获取父分组视图
@Override
public View getGroupView(final int i, boolean b, View convertView, ViewGroup viewGroup) {
final ParentViewHolder parentViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.parent_layout, null);
CheckBox parent_cb = convertView.findViewById(R.id.parent_cb);
TextView parent_title = convertView.findViewById(R.id.parent_title);
parentViewHolder = new ParentViewHolder(parent_cb, parent_title);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
// 赋值
Log.d(TAG, "getGroupView() returned: " + list.get(i).isParentIsSelected() + "---" + i);
parentViewHolder.getParent_cb().setChecked(list.get(i).isParentIsSelected());
parentViewHolder.getParent_title().setText(list.get(i).getSellerName());
// checkbox的点击事件
parentViewHolder.getParent_cb().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean selected = list.get(i).isParentIsSelected();
selected = !selected;
list.get(i).setParentIsSelected(selected);
//点击父条目的复选框,子条目相应的改变
// 将复选框的状态赋给子条目
List<CartListBean.ChildBean> list1 = MyExpanableAdapter.this.list.get(i).getList();
for (int x = 0; x < list1.size(); x++) {
CartListBean.ChildBean childBean = list1.get(x);
childBean.setChildIsSelected(selected);
}
notifyDataSetChanged();
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
});
return convertView;
}
//获取子分组视图
@Override
public View getChildView(final int i, final int i1, boolean b, View convertView, ViewGroup viewGroup) {
MyChildViewHolder myChildViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.child_layout, null);
CheckBox child_cb = convertView.findViewById(R.id.child_cb);
ImageView child_icon = convertView.findViewById(R.id.child_icon);
TextView child_price = convertView.findViewById(R.id.child_price);
TextView child_title = convertView.findViewById(R.id.child_title);
AddSubView addSubView = convertView.findViewById(R.id.add_sub_view);
myChildViewHolder = new MyChildViewHolder(child_cb, child_title, child_price, addSubView, child_icon);
convertView.setTag(myChildViewHolder);
} else {
myChildViewHolder = (MyChildViewHolder) convertView.getTag();
}
//赋值
myChildViewHolder.getChildCb().setChecked(list.get(i).getList().get(i1).isChildIsSelected());
myChildViewHolder.getChildTitle().setText(list.get(i).getList().get(i1).getTitle());
myChildViewHolder.getChildPrice().setText(list.get(i).getList().get(i1).getPrice() + "");
myChildViewHolder.getAddSubView().setCount(list.get(i).getList().get(i1).getNum());
// 设置加减按钮的点击事件
// 添加的监听
myChildViewHolder.getAddSubView().setOnAddClickListener(new AddSubView.AddClickListener() {
@Override
public void onAddClick(View view, int count) {
list.get(i).getList().get(i1).setNum(count);
//开始计算,如果选中就计算,没选中,就不计算
boolean selected = list.get(i).getList().get(i1).isChildIsSelected();
if (selected) {
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
}
});
//减的监听
myChildViewHolder.getAddSubView().setOnSubClickListener(new AddSubView.SubClickListener() {
@Override
public void onSubClick(View view, int count) {
list.get(i).getList().get(i1).setNum(count);
//开始计算,如果选中就计算,没选中,就不计算
boolean selected = list.get(i).getList().get(i1).isChildIsSelected();
if (selected) {
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
}
});
String images = list.get(i).getList().get(i1).getImages();
String icon_url = images.split("\\|")[0];
Glide.with(context).load(icon_url).into(myChildViewHolder.getChildIcon());
// 子分组的checkbox
myChildViewHolder.getChildCb().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean childIsSelected = list.get(i).getList().get(i1).isChildIsSelected();
childIsSelected = !childIsSelected;
list.get(i).getList().get(i1).setChildIsSelected(childIsSelected);
//当所有子条目被选中,所有父条目也被选中,如果有一个子条目没选中,父条目就不能被选中
List<CartListBean.ChildBean> list2 = MyExpanableAdapter.this.list.get(i).getList();
boolean flag = true;
for (int j = 0; j < list2.size(); j++) {
if (!list2.get(j).isChildIsSelected()) {
flag = false;
}
}
list.get(i).setParentIsSelected(flag);
//刷新
notifyDataSetChanged();
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
// 父分组的viewholder
class ParentViewHolder {
private CheckBox parent_cb;
private TextView parent_title;
public ParentViewHolder(CheckBox parent_cb, TextView parent_title) {
this.parent_cb = parent_cb;
this.parent_title = parent_title;
}
public CheckBox getParent_cb() {
return parent_cb;
}
public void setParent_cb(CheckBox parent_cb) {
this.parent_cb = parent_cb;
}
public TextView getParent_title() {
return parent_title;
}
public void setParent_title(TextView parent_title) {
this.parent_title = parent_title;
}
}
//创建子条目的viewholder
class MyChildViewHolder {
private CheckBox childCb;
private TextView childTitle;
private TextView childPrice;
private AddSubView addSubView;
private ImageView childIcon;
public MyChildViewHolder(CheckBox childCb, TextView childTitle, TextView childPrice, AddSubView addSubView, ImageView childIcon) {
this.childCb = childCb;
this.childTitle = childTitle;
this.childPrice = childPrice;
this.addSubView = addSubView;
this.childIcon = childIcon;
}
public CheckBox getChildCb() {
return childCb;
}
public void setChildCb(CheckBox childCb) {
this.childCb = childCb;
}
public TextView getChildTitle() {
return childTitle;
}
public void setChildTitle(TextView childTitle) {
this.childTitle = childTitle;
}
public TextView getChildPrice() {
return childPrice;
}
public void setChildPrice(TextView childPrice) {
this.childPrice = childPrice;
}
public AddSubView getAddSubView() {
return addSubView;
}
public void setAddSubView(AddSubView addSubView) {
this.addSubView = addSubView;
}
public ImageView getChildIcon() {
return childIcon;
}
public void setChildIcon(ImageView childIcon) {
this.childIcon = childIcon;
}
}
}
十八、utils包下的 SumUtils (计算价钱) 的工具类
package com.bw.lenovo.goodscart.Utils;
import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;
public class SumUtils {
public static String sum(List<CartListBean.DataBean> list){
//定义一个int类型的变量
int sum=0;
//for循环遍历list集合
for (int i=0;i<list.size();i++){
List<CartListBean.ChildBean> list1 = list.get(i).getList();
for (int j=0;j<list1.size();j++){
CartListBean.ChildBean childBean = list1.get(j);
if (childBean.isChildIsSelected()){
//把单价乘以数量的的总价赋值给sum
sum+=childBean.getPrice()*childBean.getNum();
}
}
}
return Double.toString(sum);
}
}
十九、MainActivity的主页面代码
package com.bw.lenovo.goodscart;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.bw.lenovo.goodscart.Utils.SumUtils;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.model.CartModelImpl;
import com.bw.lenovo.goodscart.presenter.PresenterImpl;
import com.bw.lenovo.goodscart.view.IView;
import com.bw.lenovo.goodscart.view.MyExpanableAdapter;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
private static final String TAG = "MainActivity=======";
private TextView sun;
private ExpandableListView expan_list_view;
private CheckBox xuan;
private Button toJisuan;
private List<CartListBean.DataBean> list;
private MyExpanableAdapter adpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化页面
initviews();
//调用presenter
PresenterImpl presenter=new PresenterImpl();
presenter.showCartList(new CartModelImpl(),this);
}
private void initviews() {
expan_list_view = (ExpandableListView) findViewById(R.id.expan_list_view);
sun = (TextView) findViewById(R.id.sum);
xuan = (CheckBox) findViewById(R.id.xuan);
toJisuan = (Button) findViewById(R.id.toJisuan);
xuan.setOnClickListener(this);
toJisuan.setOnClickListener(this);
}
// 展示的回调
@Override
public void showCartList(List<CartListBean.DataBean> list) {
this.list=list;
// 展示数据
Log.d(TAG, "showCartList() returned:" + list);
Toast.makeText(MainActivity.this,list.toString(),Toast.LENGTH_LONG).show();
adpter = new MyExpanableAdapter(MainActivity.this, list,this);
expan_list_view.setAdapter(adpter);
//展开字条目
int groupCount = expan_list_view.getCount();
for (int i = 0; i < groupCount; i++) {
expan_list_view.expandGroup(i);
}
}
//显示总价
@Override
public void showCount(String count) {
sun.setText("总价是:"+count);
String sum = SumUtils.sum(list);
}
// 点击事件
@Override
public void onClick(View view) {
switch (view.getId()) {
// 全选。反选
case R.id.xuan:
boolean checked = xuan.isChecked();
//只需要改变集合里面的数据就可以啦!然后刷新
for (int i=0;i<list.size();i++){
CartListBean.DataBean dataBean = list.get(i);
dataBean.setParentIsSelected(checked);
//获得子集合 遍历
List<CartListBean.ChildBean> list3 = dataBean.getList();
for (int j=0;j<list3.size();j++){
list3.get(j).setChildIsSelected(checked);
}
}
//刷新即可
adpter.notifyDataSetChanged();
String count = SumUtils.sum(list);
sun.setText(count);
break;
//跳转到结算页面
case R.id.toJisuan:
Toast.makeText(this, "跳转结算界面===", Toast.LENGTH_SHORT).show();
break;
}
}
}
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile files('libs/gson-2.2.4.jar')
二、添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
三、MainActivity的布局
<?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"
android:orientation="vertical"
tools:context="com.bw.lenovo.goodscart.MainActivity">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="购物车"
android:textSize="25sp"/>
<View
android:background="#999999"
android:layout_width="match_parent"
android:layout_height="0.75dp"/>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/expan_list_view"
android:groupIndicator="@null"></ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/xuan"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"/>
<TextView
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sum"
android:text="合计:¥0"
android:textSize="25sp"/>
<Button
android:id="@+id/toJisuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
android:text="去结算"/>
</LinearLayout>
</LinearLayout>
四、自定义控件(加减按钮)的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:textSize="25sp"
/>
<EditText
android:id="@+id/count"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:text="0"
android:focusable="false"
/>
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/add_sub_shape"
android:gravity="center"
android:text="+"
android:textSize="25sp"/>
</LinearLayout>
五、ExpandableListView的父视图布局
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parent_cb"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商家"
android:textSize="20sp"
android:id="@+id/parent_title"/>
</LinearLayout>
六、ExpandableListView的子视图布局
<?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="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/child_cb"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/child_icon"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品名称"
android:layout_weight="1"
android:id="@+id/child_title"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/child_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格:0"
android:textColor="@android:color/holo_red_light"/>
<com.bw.lenovo.goodscart.view.AddSubView
android:id="@+id/add_sub_view"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_marginLeft="40dp"></com.bw.lenovo.goodscart.view.AddSubView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
七、http包下的 httpConfig (接口路径)
package com.bw.lenovo.goodscart.http;
public class HttpConfig {
public static String url = "https://www.zhaoapi.cn/product/getCarts?source=android&uid=71";
}
八、http包下的 OkhttpUtils(ok封装网络框架)
package com.bw.lenovo.lianxi_goodscart.Http;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OKHttpUtils {
private static final String TAG = "OKHttpUtils===========";
private static OKHttpUtils okHttpUtils = null;
private MyHandler myhandler = new MyHandler();
private OKLoadLinsener okLoadLinsener;
public static OKHttpUtils getInstance() {
if (okHttpUtils == null) {
okHttpUtils = new OKHttpUtils();
}
return okHttpUtils;
}
public void okGet(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure() 失败=====");
Message message = myhandler.obtainMessage();
message.what = 0;
message.obj = e.getMessage();
myhandler.sendMessage(message);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "onResponse() 成功=====");
Message message = myhandler.obtainMessage();
message.what = 1;
message.obj = response.body().string();
myhandler.sendMessage(message);
}
});
}
//将输入发送到主线程,处理线程问题
public class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//获取消息的识别码 返回int类型的值
int wh = msg.what;
switch (wh) {
//失败
case 0:
String error = (String) msg.obj;
okLoadLinsener.LoadError(error);
break;
//成功
case 1:
String json = (String) msg.obj;
okLoadLinsener.LoadSuccess(json);
break;
}
}
}
//创建ok加载时的监听 (一般加载时就两种状态)失败 +成功
public interface OKLoadLinsener {
//成功
void LoadSuccess(String json);
//失败
void LoadError(String error);
}
// 设置提供一个供外部访问的方法
public void setOnLoadListener(OKLoadLinsener okLoadLinsener) {
this.okLoadLinsener = okLoadLinsener;
}
}
九、http包下的GetCartListLinsener
package com.bw.lenovo.goodscart.http;
import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;
/**
* 获取购物车的监听
*/
public interface GetCartListListener {
void getCartSuccess(List<CartListBean.DataBean> list);
// 失败
void getCartError(String error);
}
十、model包下需要写一个Bean类==解析接口数据(一键解析)
注:这个购物车接口有一个为空(需要手动添加一个childBean)把?问号替换成childBean
十一、model包下写ICartModel
package com.bw.lenovo.goodscart.model;
import com.bw.lenovo.goodscart.http.GetCartListListener;
/**
* 去请求购物车数据
*/
public interface ICartModel {
void getCartList(String url, GetCartListListener getCartListListener);
}
十二、model包下写 CartModelmpI
package com.bw.lenovo.goodscart.model;
import android.util.Log;
import com.bw.lenovo.goodscart.http.GetCartListListener;
import com.bw.lenovo.goodscart.http.HttpConfig;
import com.bw.lenovo.goodscart.http.OKHttpUtils;
import com.google.gson.Gson;
import java.util.List;
import static android.content.ContentValues.TAG;
public class CartModelImpl implements ICartModel {
private static final String TAG = "CartModelImpl======";
@Override
public void getCartList(String url, final GetCartListListener getCartListListener) {
OKHttpUtils okHttpUtils = OKHttpUtils.getInstance();
okHttpUtils.okGet(url);
okHttpUtils.setOnLoadListener(new OKHttpUtils.OKLoadListener() {
@Override
public void loadSuccess(String json) {
Log.d(TAG, "成功" + json);
//网络请求成功
Gson gson = new Gson();
CartListBean cartListBean = gson.fromJson(json, CartListBean.class);
List<CartListBean.DataBean> list = cartListBean.getData();
// 对象
getCartListListener.getCartSuccess(list);
}
@Override
public void loadError(String error) {
//网络请求失败
getCartListListener.getCartError(error);
}
});
}
}
十三、view包下的IView
package com.bw.lenovo.goodscart.view;
import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;
public interface IView {
//展示
void showCartList(List<CartListBean.DataBean> list);
//显示总价
void showCount(String count);
}
十四、IView包下的自定义控件(加减控件)
package com.bw.lenovo.goodscart.view;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bw.lenovo.goodscart.R;
public class AddSubView extends LinearLayout implements View.OnClickListener {
private static final String TAG = "AddSubView=============";
private TextView sub;
private EditText count;
private TextView add;
private AddClickListener addClickListener;
private SubClickListener subClickListener;
public AddSubView(Context context) {
this(context, null);
}
public AddSubView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.add_sub_layout, this);
//获取控件
sub = view.findViewById(R.id.sub);
count = view.findViewById(R.id.count);
add = view.findViewById(R.id.add);
sub.setOnClickListener(this);
add.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View view) {
switch (view.getId()) {
// 当id为减时
case R.id.sub:
// 获取当时输入的内容
String c = count.getText().toString();
// 装换成int类型
int i = Integer.parseInt(c);
// 判断i小于0的时候
if (i <= 0) {
// 停止
return;
}
count.setText(--i + "");
subClickListener.onSubClick(view, i);
Log.d(TAG, "减=======" + i);
break;
case R.id.add:
String c1 = count.getText().toString();
int i1 = Integer.parseInt(c1);
count.setText(++i1 + "");
addClickListener.onAddClick(view, i1);
Log.d(TAG, "onClick() returned:" + "加=======");
break;
}
}
//获取内容的方法
public int getCount() {
//返回输入的内容转换成int类型的去掉空格
return Integer.parseInt(count.getText().toString().trim());
}
public void setCount(int s) {
count.setText(s + "");
}
//定义一个可被外部调用的接口(加)
public interface AddClickListener {
void onAddClick(View view, int count);
}
// 定义一个可被外部调用的接口(减)
public interface SubClickListener {
void onSubClick(View view, int count);
}
//加的点击事件
public void setOnAddClickListener(AddClickListener addClickListener) {
this.addClickListener = addClickListener;
}
// 减的点击事件
public void setOnSubClickListener(SubClickListener subClickListener) {
this.subClickListener = subClickListener;
}
}
十五、presenter包下的IPresenter
package com.bw.lenovo.goodscart.presenter;
import com.bw.lenovo.goodscart.model.ICartModel;
import com.bw.lenovo.goodscart.view.IView;
public interface IPresenter {
// 展示数据
void showCartList(ICartModel iCartModel, IView iView);
}
十六、presenter包下的presenterImpI
package com.bw.lenovo.goodscart.presenter;
import android.net.IpPrefix;
import com.bw.lenovo.goodscart.http.GetCartListListener;
import com.bw.lenovo.goodscart.http.HttpConfig;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.model.ICartModel;
import com.bw.lenovo.goodscart.view.IView;
import java.util.List;
public class PresenterImpl implements IPresenter{
@Override
public void showCartList(ICartModel iCartModel, final IView iView) {
// 具体的操作
iCartModel.getCartList(HttpConfig.url, new GetCartListListener() {
@Override
public void getCartSuccess(List<CartListBean.DataBean> list) {
// 传给view
iView.showCartList(list);
}
@Override
public void getCartError(String error) {
}
});
}
}
十七、view包下的 ExpandableListView的适配器
package com.bw.lenovo.goodscart.view;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.NoAnimation;
import com.bw.lenovo.goodscart.R;
import com.bw.lenovo.goodscart.Utils.SumUtils;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.presenter.PresenterImpl;
import java.util.List;
public class MyExpanableAdapter extends BaseExpandableListAdapter {
private final Context context;
private static final String TAG = "MyExpanableAdapter----";
private final List<CartListBean.DataBean> list;
private final IView iView;
public MyExpanableAdapter(Context context, List<CartListBean.DataBean> list, IView iView) {
this.context = context;
this.list = list;
this.iView = iView;
}
// 获取父分组的数量
@Override
public int getGroupCount() {
return list.size();
}
// 获取子分组的数量
@Override
public int getChildrenCount(int i) {
return list.get(i).getList().size();
}
//获取父分组的对象
@Override
public Object getGroup(int i) {
return list.get(i);
}
//获取子分组的对象
@Override
public Object getChild(int i, int i1) {
return list.get(i).getList().get(i1);
}
//获取父分组的id
@Override
public long getGroupId(int i) {
return i;
}
//获取子分组的id
@Override
public long getChildId(int i, int i1) {
return i1;
}
//保持平衡的意思
@Override
public boolean hasStableIds() {
return false;
}
// 获取父分组视图
@Override
public View getGroupView(final int i, boolean b, View convertView, ViewGroup viewGroup) {
final ParentViewHolder parentViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.parent_layout, null);
CheckBox parent_cb = convertView.findViewById(R.id.parent_cb);
TextView parent_title = convertView.findViewById(R.id.parent_title);
parentViewHolder = new ParentViewHolder(parent_cb, parent_title);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
// 赋值
Log.d(TAG, "getGroupView() returned: " + list.get(i).isParentIsSelected() + "---" + i);
parentViewHolder.getParent_cb().setChecked(list.get(i).isParentIsSelected());
parentViewHolder.getParent_title().setText(list.get(i).getSellerName());
// checkbox的点击事件
parentViewHolder.getParent_cb().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean selected = list.get(i).isParentIsSelected();
selected = !selected;
list.get(i).setParentIsSelected(selected);
//点击父条目的复选框,子条目相应的改变
// 将复选框的状态赋给子条目
List<CartListBean.ChildBean> list1 = MyExpanableAdapter.this.list.get(i).getList();
for (int x = 0; x < list1.size(); x++) {
CartListBean.ChildBean childBean = list1.get(x);
childBean.setChildIsSelected(selected);
}
notifyDataSetChanged();
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
});
return convertView;
}
//获取子分组视图
@Override
public View getChildView(final int i, final int i1, boolean b, View convertView, ViewGroup viewGroup) {
MyChildViewHolder myChildViewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.child_layout, null);
CheckBox child_cb = convertView.findViewById(R.id.child_cb);
ImageView child_icon = convertView.findViewById(R.id.child_icon);
TextView child_price = convertView.findViewById(R.id.child_price);
TextView child_title = convertView.findViewById(R.id.child_title);
AddSubView addSubView = convertView.findViewById(R.id.add_sub_view);
myChildViewHolder = new MyChildViewHolder(child_cb, child_title, child_price, addSubView, child_icon);
convertView.setTag(myChildViewHolder);
} else {
myChildViewHolder = (MyChildViewHolder) convertView.getTag();
}
//赋值
myChildViewHolder.getChildCb().setChecked(list.get(i).getList().get(i1).isChildIsSelected());
myChildViewHolder.getChildTitle().setText(list.get(i).getList().get(i1).getTitle());
myChildViewHolder.getChildPrice().setText(list.get(i).getList().get(i1).getPrice() + "");
myChildViewHolder.getAddSubView().setCount(list.get(i).getList().get(i1).getNum());
// 设置加减按钮的点击事件
// 添加的监听
myChildViewHolder.getAddSubView().setOnAddClickListener(new AddSubView.AddClickListener() {
@Override
public void onAddClick(View view, int count) {
list.get(i).getList().get(i1).setNum(count);
//开始计算,如果选中就计算,没选中,就不计算
boolean selected = list.get(i).getList().get(i1).isChildIsSelected();
if (selected) {
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
}
});
//减的监听
myChildViewHolder.getAddSubView().setOnSubClickListener(new AddSubView.SubClickListener() {
@Override
public void onSubClick(View view, int count) {
list.get(i).getList().get(i1).setNum(count);
//开始计算,如果选中就计算,没选中,就不计算
boolean selected = list.get(i).getList().get(i1).isChildIsSelected();
if (selected) {
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
}
});
String images = list.get(i).getList().get(i1).getImages();
String icon_url = images.split("\\|")[0];
Glide.with(context).load(icon_url).into(myChildViewHolder.getChildIcon());
// 子分组的checkbox
myChildViewHolder.getChildCb().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean childIsSelected = list.get(i).getList().get(i1).isChildIsSelected();
childIsSelected = !childIsSelected;
list.get(i).getList().get(i1).setChildIsSelected(childIsSelected);
//当所有子条目被选中,所有父条目也被选中,如果有一个子条目没选中,父条目就不能被选中
List<CartListBean.ChildBean> list2 = MyExpanableAdapter.this.list.get(i).getList();
boolean flag = true;
for (int j = 0; j < list2.size(); j++) {
if (!list2.get(j).isChildIsSelected()) {
flag = false;
}
}
list.get(i).setParentIsSelected(flag);
//刷新
notifyDataSetChanged();
//计算
String sum = SumUtils.sum(list);
iView.showCount(sum);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
// 父分组的viewholder
class ParentViewHolder {
private CheckBox parent_cb;
private TextView parent_title;
public ParentViewHolder(CheckBox parent_cb, TextView parent_title) {
this.parent_cb = parent_cb;
this.parent_title = parent_title;
}
public CheckBox getParent_cb() {
return parent_cb;
}
public void setParent_cb(CheckBox parent_cb) {
this.parent_cb = parent_cb;
}
public TextView getParent_title() {
return parent_title;
}
public void setParent_title(TextView parent_title) {
this.parent_title = parent_title;
}
}
//创建子条目的viewholder
class MyChildViewHolder {
private CheckBox childCb;
private TextView childTitle;
private TextView childPrice;
private AddSubView addSubView;
private ImageView childIcon;
public MyChildViewHolder(CheckBox childCb, TextView childTitle, TextView childPrice, AddSubView addSubView, ImageView childIcon) {
this.childCb = childCb;
this.childTitle = childTitle;
this.childPrice = childPrice;
this.addSubView = addSubView;
this.childIcon = childIcon;
}
public CheckBox getChildCb() {
return childCb;
}
public void setChildCb(CheckBox childCb) {
this.childCb = childCb;
}
public TextView getChildTitle() {
return childTitle;
}
public void setChildTitle(TextView childTitle) {
this.childTitle = childTitle;
}
public TextView getChildPrice() {
return childPrice;
}
public void setChildPrice(TextView childPrice) {
this.childPrice = childPrice;
}
public AddSubView getAddSubView() {
return addSubView;
}
public void setAddSubView(AddSubView addSubView) {
this.addSubView = addSubView;
}
public ImageView getChildIcon() {
return childIcon;
}
public void setChildIcon(ImageView childIcon) {
this.childIcon = childIcon;
}
}
}
十八、utils包下的 SumUtils (计算价钱) 的工具类
package com.bw.lenovo.goodscart.Utils;
import com.bw.lenovo.goodscart.model.CartListBean;
import java.util.List;
public class SumUtils {
public static String sum(List<CartListBean.DataBean> list){
//定义一个int类型的变量
int sum=0;
//for循环遍历list集合
for (int i=0;i<list.size();i++){
List<CartListBean.ChildBean> list1 = list.get(i).getList();
for (int j=0;j<list1.size();j++){
CartListBean.ChildBean childBean = list1.get(j);
if (childBean.isChildIsSelected()){
//把单价乘以数量的的总价赋值给sum
sum+=childBean.getPrice()*childBean.getNum();
}
}
}
return Double.toString(sum);
}
}
十九、MainActivity的主页面代码
package com.bw.lenovo.goodscart;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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.bw.lenovo.goodscart.Utils.SumUtils;
import com.bw.lenovo.goodscart.model.CartListBean;
import com.bw.lenovo.goodscart.model.CartModelImpl;
import com.bw.lenovo.goodscart.presenter.PresenterImpl;
import com.bw.lenovo.goodscart.view.IView;
import com.bw.lenovo.goodscart.view.MyExpanableAdapter;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView, View.OnClickListener {
private static final String TAG = "MainActivity=======";
private TextView sun;
private ExpandableListView expan_list_view;
private CheckBox xuan;
private Button toJisuan;
private List<CartListBean.DataBean> list;
private MyExpanableAdapter adpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化页面
initviews();
//调用presenter
PresenterImpl presenter=new PresenterImpl();
presenter.showCartList(new CartModelImpl(),this);
}
private void initviews() {
expan_list_view = (ExpandableListView) findViewById(R.id.expan_list_view);
sun = (TextView) findViewById(R.id.sum);
xuan = (CheckBox) findViewById(R.id.xuan);
toJisuan = (Button) findViewById(R.id.toJisuan);
xuan.setOnClickListener(this);
toJisuan.setOnClickListener(this);
}
// 展示的回调
@Override
public void showCartList(List<CartListBean.DataBean> list) {
this.list=list;
// 展示数据
Log.d(TAG, "showCartList() returned:" + list);
Toast.makeText(MainActivity.this,list.toString(),Toast.LENGTH_LONG).show();
adpter = new MyExpanableAdapter(MainActivity.this, list,this);
expan_list_view.setAdapter(adpter);
//展开字条目
int groupCount = expan_list_view.getCount();
for (int i = 0; i < groupCount; i++) {
expan_list_view.expandGroup(i);
}
}
//显示总价
@Override
public void showCount(String count) {
sun.setText("总价是:"+count);
String sum = SumUtils.sum(list);
}
// 点击事件
@Override
public void onClick(View view) {
switch (view.getId()) {
// 全选。反选
case R.id.xuan:
boolean checked = xuan.isChecked();
//只需要改变集合里面的数据就可以啦!然后刷新
for (int i=0;i<list.size();i++){
CartListBean.DataBean dataBean = list.get(i);
dataBean.setParentIsSelected(checked);
//获得子集合 遍历
List<CartListBean.ChildBean> list3 = dataBean.getList();
for (int j=0;j<list3.size();j++){
list3.get(j).setChildIsSelected(checked);
}
}
//刷新即可
adpter.notifyDataSetChanged();
String count = SumUtils.sum(list);
sun.setText(count);
break;
//跳转到结算页面
case R.id.toJisuan:
Toast.makeText(this, "跳转结算界面===", Toast.LENGTH_SHORT).show();
break;
}
}
}
二十、自定义按钮里面的背景框(在drawable->Drawable resouce file)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="100dp"
android:height="30dp"/>
<stroke
android:width="1dp"
android:color="#999999"/>
</shape>
注:在子布局导入自定义控件时,用自己包下的