复制即用,阅读即懂
需要注入依赖和网络权限ImageLoader的App
//Ok请求
compile 'com.squareup.okhttp3:okhttp:3.9.1'
//Gson解析
compile 'com.google.code.gson:gson:2.2.4'
//EventBus传递
compile 'org.greenrobot:eventbus:3.1.1'
//ImageLoader
compile files('libs/universal-image-loader-1.9.5.jar')
主布局头部布局加二级列表(activity_main)
商家店铺名称,一级标题布局(layout_group)
商品列表,二级标题布局(layout_child)
自定义拦截器(Intercept)
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public class Intercept implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl url=original.url().newBuilder()
.addQueryParameter("source","android")
.build();
//添加请求头
Request request = original.newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
}
Ok封装的工具请求类(OKHttpUtils)
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
import android.os.Handler;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* author:Created by WangZhiQiang on 2017/12/5.
*/
public class OKHttpUtils {
private static OKHttpUtils okHttpUtils;
private static Handler handler = new Handler();
//私有化构造方法
public OKHttpUtils() {
}
/**
* 获取OKhttpClient实例化
*/
public static OKHttpUtils getInstance() {
if (null == okHttpUtils) {
synchronized (OKHttpUtils.class) {
if (null == okHttpUtils) {
okHttpUtils = new OKHttpUtils();
}
}
}
return okHttpUtils;
}
/**
* 封装的异步Get请求
*/
public void doGet(String path, Map map, final IOkHttpUtils okHttpCallBack) {
//创建一个字符串容器
StringBuilder sb = null;
if (map.size() == 0) {
if (null == sb) {
sb = new StringBuilder();
sb.append(path);
}
} else {
for (String key : map.keySet()) {
if (null == sb) {
sb = new StringBuilder();
sb.append(path);
sb.append("?");
} else {
sb.append("&");
}
sb.append(key).append("=").append(map.get(key));
}
}
//System.out.println("分类 : "+path + sb.toString());
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Intercept())//使用拦截器
.build();
Request request = new Request.Builder()
.url(sb.toString())
.get()
.build();
//OKHttp 网络
Call call = okHttpClient.newCall(request);
//异步请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败
Log.e("SKN", "OK请求失败");
okHttpCallBack.onFailed(e.getMessage());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String str = response.body().string();
//请求成功
handler.post(new Runnable() {
@Override
public void run() {
Log.e("WWWW", "请求成功" + str);
okHttpCallBack.onSuccess(str);
}
});
}
});
}
}
Ok封装工具类的接口(IOkHttpUtils)
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
/**
* author:Created by Androidon 2017/12/10.
*/
public interface IOkHttpUtils {
void onSuccess(String str);
void onFailed(String message);
}
ImageLoader工具封装类
import android.app.Application;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public class ImageApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(this)
.memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽
.threadPoolSize(5)//线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
.writeDebugLogs() // Remove for release app
.build();//开始构建
//初始化ImageLoader
ImageLoader.getInstance().init(config);
}
}
Json串的Bean类(JsonBean)
import java.util.List;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public class JsonBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":7,"pid":90,"price":1233,"pscid":112,"selected":0,"sellerid":1,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":1,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":91,"price":1344,"pscid":112,"selected":0,"sellerid":2,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"}],"sellerName":"商家2","sellerid":"2"}]
*/
private String msg;
private String code;
private List data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* list : [{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":7,"pid":90,"price":1233,"pscid":112,"selected":0,"sellerid":1,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":1,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}]
* sellerName : 商家1
* sellerid : 1
*/
private boolean checked;
private String sellerName;
private String sellerid;
private List list;
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public static class ListBean {
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* bargainPrice : 22.9
* createtime : 2017-10-14T21:48:08
* detailUrl : https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg
* num : 1
* pid : 24
* price : 288.0
* pscid : 2
* selected : 0
* sellerid : 1
* subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》
* title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋
*/
private boolean checked;
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private double price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
主布局(MainActivity)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IShopCarPresenter {
private ExpandableListView ex;
private CheckBox selectAll;
private TextView price;
private TextView count;
private ShopCarPresenter shopCarPresenter;
private int uid = 3384;
private List groupList = new ArrayList<>();
private List> childList = new ArrayList<>();
private MyExAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();//初始化视图
/**
* 用于传控件,
* 需要导入依赖才可以进行传送
*/
EventBus.getDefault().register(this); //全选/全不选
selectAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置全选
myAdapter.changeAllListCbState(selectAll.isChecked());
}
});
}
//每次进这个页面都要甩你
@Override
protected void onResume() {
super.onResume();
initData();//初始化数据
}
//初始化数据
private void initData() {
//实例化P层
shopCarPresenter = new ShopCarPresenter(this);
shopCarPresenter.receive(uid + "");
}
//初始化集合
private void initView() {
ex = (ExpandableListView) findViewById(R.id.ex);//二级列表
selectAll = (CheckBox) findViewById(R.id.selectAll);//全选按钮
price = (TextView) findViewById(R.id.price);//价格
count = (TextView) findViewById(R.id.count);//数量
}
@Override
public void onSuccess(List data) {
//清空一下多余的列表展示
groupList.clear();
childList.clear();
//将商家的数据加入到大集合grouList
groupList.addAll(data);
//将商家下商品的数据全部添加到小集合childList
for (int i = 0; i < data.size(); i++) {
List list = data.get(i).getList();
childList.add(list);
}
/**
* 配置适配器
* 将商家的大集合和商家下的商品小集合全部传到适配器
*/
myAdapter = new MyExAdapter(MainActivity.this, groupList, childList);
ex.setAdapter(myAdapter);
//二级列表默认展开
for (int i = 0; i < data.size(); i++) {
ex.expandGroup(i);//二级列表自带的方法
}
//刷新一下适配器
myAdapter.notifyDataSetChanged();
}
@Override
public void onFailed() {
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
selectAll.setChecked(event.isCheckd());
}
//计算总价和总数量
@Subscribe
public void onMessageEven(PriceAndCountEvent event) {
price.setText(event.getPrice() + ".00");
count.setText("(" + event.getCount() + ")");
}
}
P层(ShopCarPresenter)
import java.util.List;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public class ShopCarPresenter implements IShopCarModel {
private ShopCarModel shopCarModel;
private IShopCarPresenter iShopCarPresenter;
public ShopCarPresenter(IShopCarPresenter iShopCarPresenter) {
this.iShopCarPresenter = iShopCarPresenter;
shopCarModel = new ShopCarModel();
}
public void receive(String uid) {
shopCarModel.receive(uid,this);
}
@Override
public void onSuccess(List data) {
iShopCarPresenter.onSuccess(data);
}
@Override
public void onFailed() {
iShopCarPresenter.onFailed();
}
}
M层(ShopCarModel)
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public class ShopCarModel {
public void receive(String uid, final IShopCarModel iShopCarModel) {
OKHttpUtils instance = OKHttpUtils.getInstance();
HashMap hashMap = new HashMap<>();
hashMap.put("uid",uid);
instance.doGet("http://120.27.23.105/product/getCarts", hashMap, new IOkHttpUtils() {
@Override
public void onSuccess(String str) {
if(str!=null){
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(str, JsonBean.class);
if(jsonBean!=null){
List data = jsonBean.getData();
iShopCarModel.onSuccess(data);
}
}
}
@Override
public void onFailed(String message) {
iShopCarModel.onFailed();
}
});
}
}
M层传值的接口(IShopCarModel)
import java.util.List;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public interface IShopCarModel {
void onSuccess(List data);
void onFailed();
}
P层传值的接口(IShopCarPresenter)
import java.util.List;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public interface IShopCarPresenter {
void onSuccess(List data);
void onFailed();
}
Event用于在适配器和Activity之间传送接收控件的适配器(MessageEvent)
/**
* Created by HASEE on 2017/11/22.
*/
public class MessageEvent {
private boolean checkd;
public boolean isCheckd(){
return checkd;
}
public void setCheckd(boolean checkd){
this.checkd=checkd;
}
}
计算选中数量和价格的适配器(PriceAndCountEvent)
/**
* Created by HASEE on 2017/11/22.
*/
public class PriceAndCountEvent {
private int price;
private int count;
private int to;
public int getPrice() {
return price;
}
public int getCount() {
return count;
}
public int getTo(){
return to;
}
public void setPrice(int price) {
this.price = price;
}
public void setCount(int count) {
this.count = count;
}
public void setTo(int to) {
this.to = to;
}
}
ExpandAbleListView二级列表的适配器(MyExAdapter)
import android.content.Context;
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.nostra13.universalimageloader.core.ImageLoader;
import org.greenrobot.eventbus.EventBus;
import java.util.List;
/**
* author:Created by ZhangPengFei on 2017/12/19.
*/
public class MyExAdapter extends BaseExpandableListAdapter {
private Context context;
private List groupList;
private List> childList;
public MyExAdapter(Context context, List groupList, List> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int i) {
return childList.get(i).size();
}
@Override
public Object getGroup(int i) {
return groupList.get(i);
}
@Override
public Object getChild(int i, int i1) {
return childList.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
/**
* 装载一级列表数据
* 商家的全选框
* 商家名称
*/
class ViewHolder {
CheckBox checkGroup;
TextView storeName;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = View.inflate(context, R.layout.layout_group, null);
holder.checkGroup = view.findViewById(R.id.checkGroup);
holder.storeName = view.findViewById(R.id.storeName);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.storeName.setText(groupList.get(i).getSellerName());
//设置以及列表的点击状态
holder.checkGroup.setChecked(groupList.get(i).isChecked());
//一级列表checkBox的点击事件
holder.checkGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断一级列表复选框的状态,设置为true或false
groupList.get(i).setChecked(holder.checkGroup.isChecked());
//改变二级checkBox的状态
changeChildState(i, holder.checkGroup.isChecked());
//算钱
EventBus.getDefault().post(computer());
//改变全选状态 isAllGroupCbSelect判断一级是否全部选中
changeAllState(isAllGroupSelect());
//必刷新
notifyDataSetChanged();
}
});
return view;
}
/**
* 装载数据
* 装载当前店铺下的
* 要展示的商品
*/
class ViewHolder1 {
CheckBox checkChild;
ImageView img;
TextView title;
TextView price;
TextView lessen;
TextView count;
TextView add;
Button btnDelete;
}
@Override
public View getChildView(final int i, int i1, boolean b, View view, ViewGroup viewGroup) {
final JsonBean.DataBean.ListBean bean = childList.get(i).get(i1);
final ViewHolder1 holder1;
/**
* 优化,判断为空
*/
if (view == null) {
holder1 = new ViewHolder1();
//获取视图
view = View.inflate(context, R.layout.layout_child, null);
/**
* 获取ID
*/
holder1.checkChild = view.findViewById(R.id.checkChild);//选框
holder1.img = view.findViewById(R.id.img);//图片
holder1.title = view.findViewById(R.id.title);//标题
holder1.price = view.findViewById(R.id.price);//价格
holder1.lessen = view.findViewById(R.id.lessen);//减
holder1.count = view.findViewById(R.id.count);//数量
holder1.add = view.findViewById(R.id.add);//加
holder1.btnDelete = view.findViewById(R.id.btnDelete);//删除的按钮
view.setTag(holder1);
} else {
holder1 = (ViewHolder1) view.getTag();
}
ImageLoader.getInstance().displayImage(bean.getImages(), holder1.img);
holder1.title.setText(bean.getTitle());
holder1.price.setText(bean.getPrice() + "");
holder1.count.setText(bean.getNum() + "");
//设置二级列表checkbox的属性
holder1.checkChild.setChecked(childList.get(i).get(i1).isChecked());
//二级列表的点击事件
holder1.checkChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置该条目中的checkbox属性值
bean.setChecked(holder1.checkChild.isChecked());
//计算价钱
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
//判断当前checkbox是选中的状态
if (holder1.checkChild.isChecked()) {
//如果全部选中(isAllChildCbSelected)
if (isAllChildCbSelected(i)) {
//改变一级列表的状态
changeGroupState(i, true);
//改变全选的状态
changeAllState(isAllGroupSelect());
}
} else {
//如果没有全部选中,一级列表的checkbox为false不为选中
changeGroupState(i, false);
changeAllState(isAllGroupSelect());
}
notifyDataSetChanged();
}
});//加号
holder1.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = bean.getNum();
//num为int类型所以要加空字符串
holder1.count.setText(++num + "");
bean.setNum(num);
//如果二级列表的checkbox为选中,计算价钱
if (holder1.checkChild.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//减号
holder1.lessen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = bean.getNum();
if (num == 1) {
return;
}
holder1.count.setText(--num + "");
bean.setNum(num);
if (holder1.checkChild.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
//改变二级列表的checkBox的状态,如果一级选中,控制二级也选中
private void changeChildState(int i, boolean flag) {
List listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
JsonBean.DataBean.ListBean listBean = listBeen.get(j);
listBean.setChecked(flag);
}
}
//判断一级列表是否全部选中
public boolean isAllGroupSelect() {
for (int i = 0; i < childList.size(); i++) {
JsonBean.DataBean dataBean = groupList.get(i);
if (!dataBean.isChecked()) {
return false;
}
}
return true;
}
//改变全选的状态
private void changeAllState(boolean flag) {
//导入的类
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheckd(flag);
EventBus.getDefault().post(messageEvent);
}
//改变列表的checkBox的状态
private void changeGroupState(int i,boolean flag){
JsonBean.DataBean dataBean = groupList.get(i);
dataBean.setChecked(flag);
}
//判断二级列表是否全部选中
private boolean isAllChildCbSelected(int i) {
List listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
JsonBean.DataBean.ListBean listBean = listBeen.get(j);
if (!listBean.isChecked()) {
return false;
}
}
return true;
}
//设置全选,反选
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < childList.size(); i++) {
changeGroupState(i, flag);
changeChildState(i, flag);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
//计算列表的价钱
private PriceAndCountEvent computer() {
int count = 0;
int price = 0;
int to = 0;
for (int i = 0; i < childList.size(); i++) {
List listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
JsonBean.DataBean.ListBean listBean = listBeen.get(j);
if (listBean.isChecked()) {
price += listBean.getNum() * listBean.getPrice();
count += listBean.getNum();
to += listBean.getNum();
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
priceAndCountEvent.setTo(to);
return priceAndCountEvent;
}
}
如果JsonBean类里面没有isCheck的有参需要手动添加
//放入DateBean
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
//放入ListBean
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}