Android仿京東Appショッピングカート二級リスト+全選逆選+Okパッケージ+ブロック+決済+商品数量計算(完全コード)
43919 ワード
コピーしてすぐ使う、読むとすぐわかる
依存とネットワーク権限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')
メインレイアウトヘッダレイアウトに2次リスト(activity_main)を付ける
店舗名、一級タイトルレイアウト(layout_group)
商品リスト、二級タイトルレイアウト(layout_child)
Okパッケージのツール要求クラス(OKHttpUtils)
Okパッケージツールクラスのインタフェース(IOkHttpUtils)
ImageLoaderツールパッケージクラス
Json列のBeanクラス(JsonBean)
メインレイアウト(MainActivity)
P層(ShopCarPresenter)
M層(ShopCarModel)
M層伝達インタフェース(IShopCarModel)
P層伝達インタフェース(IShopCarPresenter)
EventアダプタとActivityの間で受信コントロールを転送するためのアダプタ(MessageEvent)
選択した数と価格を計算するアダプタ(PriceAndCountEvent)
ExpandAbleListView 2レベルリストのアダプタ(MyExAdapter)
JsonBeanクラスにisCheckのパラメータがない場合は手動で追加する必要があります
依存とネットワーク権限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')
メインレイアウトヘッダレイアウトに2次リスト(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
"}],"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
"}]
* 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 2レベルリストのアダプタ(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;
}