创建两个bean,方便EventBus传值
public class CountPrice {===============总价钱
private int count;
private int price;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
public class MsgEvent {============boolean类型的复选框
private boolean checked;
public MsgEvent(boolean checked) {
this.checked = checked;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
M层
m层接口
public interface IModelListener {
void OnModel(ProductBean productBean);
}
public interface IModel {
void OnShopM(IModelListener Im,int uid);
}
M层类
public class ShopModel implements IModel {
@Override
public void OnShopM(final IModelListener Im, int uid) {
HttpManager.init(ContastUrl.url);
ApiService apiService = HttpManager.getInstance().create(ApiService.class);
Observable<ProductBean> proService = apiService.getProService(uid);
proService.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ProductBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ProductBean productBean) {
Im.OnModel(productBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
V层
public interface IView {
void success(List<ProductBean.DataBean> proList);
}
P层
public class ShopPresenter {
private IView iView;
private ShopModel shopModel;
public ShopPresenter(IView iView) {
this.iView = iView;
shopModel = new ShopModel();
}
public void getShopData(int uid) {
shopModel.OnShopM(new IModelListener() {
@Override
public void OnModel(ProductBean productBean) {
List<ProductBean.DataBean> data = productBean.getData();
iView.success(data);
}
}, uid);
}
}
接口地址
public interface ApiService {
@GET("getCarts")
Observable<ProductBean> getProService(@Query("uid") int uid);
}
总地址
public class ContastUrl {
public static final String url = "http://www.zhaoapi.cn/product/";
}
工具类
public class HttpManager {
private static HttpManager mHttpManager;
private static Retrofit retrofit;
public static HttpManager getInstance() {
if (mHttpManager == null) {
synchronized (HttpManager.class) {
if (mHttpManager == null) {
return mHttpManager = new HttpManager();
}
}
}
return mHttpManager;
}
public static HttpManager init(String url) {
OkHttpClient httpClient = new OkHttpClient.Builder()
.readTimeout(3000, TimeUnit.MILLISECONDS)
.writeTimeout(3000, TimeUnit.MILLISECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(url)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return null;
}
public <T> T create(Class<T> clazz) {
return retrofit.create(clazz);
}
}
自定义View加减器
public class MyAddSubView extends LinearLayout implements View.OnClickListener {
private Button addBtn;
private Button subBtn;
private TextView numberText;
private int number = 1;
public MyAddSubView(Context context) {
this(context, null);
}
public MyAddSubView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyAddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = inflate(context, R.layout.add_sub_item, this);
addBtn = view.findViewById(R.id.add_btn);
subBtn = view.findViewById(R.id.sub_btn);
numberText = view.findViewById(R.id.number_text);
addBtn.setOnClickListener(this);
subBtn.setOnClickListener(this);
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
numberText.setText("" + number);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_btn:
++number;
numberText.setText("" + number);
if (mOnChangAddSubListener != null) {
mOnChangAddSubListener.onChangNumber(number);
}
break;
case R.id.sub_btn:
if (number > 1) {
--number;
numberText.setText("" + number);
if (mOnChangAddSubListener != null) {
mOnChangAddSubListener.onChangNumber(number);
}
} else {
Toast.makeText(getContext(), "不能再少了", Toast.LENGTH_SHORT).show();
}
break;
}
}
public interface OnChangAddSubListener {
void onChangNumber(int num);
}
private OnChangAddSubListener mOnChangAddSubListener;
public void setOnChangAddSubListener(OnChangAddSubListener onChangAddSubListener) {
mOnChangAddSubListener = onChangAddSubListener;
}
}
ShopAdapter
public class ShopAdapter extends BaseExpandableListAdapter {
private List<ProductBean.DataBean> proList;
public ShopAdapter(List<ProductBean.DataBean> proList) {
this.proList = proList;
}
@Override
public int getGroupCount() {
return proList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return proList.get(groupPosition).getList().size();
}
//=================组布局(商家)===========
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final GroupHolder gh;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.prepare_item, null);
gh = new GroupHolder();
gh.group_box = convertView.findViewById(R.id.group_box);
gh.group_name = convertView.findViewById(R.id.group_name);
convertView.setTag(gh);
} else {
gh = (GroupHolder) convertView.getTag();
}
//获取商家信息
ProductBean.DataBean dataBean = proList.get(groupPosition);
gh.group_name.setText(dataBean.getSellerName());
boolean childSelected = isChildSelected(groupPosition);
//商品选中,商家选中
gh.group_box.setChecked(childSelected);
gh.group_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取当前状态
boolean checked = gh.group_box.isChecked();
//控制商品全选(子条目)
isCheckChang(groupPosition,checked);
//计算总价和数量
EventBus.getDefault().post(TotalAll());
notifyDataSetChanged();
}
});
return convertView;
}
//=================子布局(商品)===============
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ChildHolder ch;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.children_item, null);
ch = new ChildHolder();
ch.children_addsub = convertView.findViewById(R.id.children_addsub);
ch.children_box = convertView.findViewById(R.id.children_box);
ch.children_content = convertView.findViewById(R.id.children_content);
ch.children_image = convertView.findViewById(R.id.children_image);
ch.children_price = convertView.findViewById(R.id.children_price);
convertView.setTag(ch);
} else {
ch = (ChildHolder) convertView.getTag();
}
//获取商品的信息
List<ProductBean.DataBean.ListBean> list = proList.get(groupPosition).getList();
final ProductBean.DataBean.ListBean listBean = list.get(childPosition);
ch.children_content.setText(listBean.getTitle());
ch.children_image.setImageURI(listBean.getImages().split("\\|")[0]);
ch.children_price.setText(listBean.getPrice() + "");
ch.children_addsub.setNumber(listBean.getNum());
ch.children_box.setChecked(listBean.getSelected()==1);
//加减器监听
ch.children_addsub.setOnChangAddSubListener(new MyAddSubView.OnChangAddSubListener() {
@Override
public void onChangNumber(int num) {
//设置加减值
changAddSubNumber(groupPosition,childPosition,num);
EventBus.getDefault().post(TotalAll());
//更新适配器
notifyDataSetChanged();
}
});
ch.children_box.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取当前状态值
boolean checked = ch.children_box.isChecked();
if (checked){
listBean.setSelected(1);
}else{
listBean.setSelected(0);
}
EventBus.getDefault().post(TotalAll());
notifyDataSetChanged();
}
});
return convertView;
}
/*通过商家控制商品*/
private void isCheckChang(int i, boolean flag) {
ProductBean.DataBean dataBean = proList.get(i);
List<ProductBean.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(flag ? 1 : 0);
}
}
/*判断所有的复选框是否选中*/
public boolean isAllCheckBox(){
for (int i=0;i<proList.size();i++){
ProductBean.DataBean dataBean = proList.get(i);
List<ProductBean.DataBean.ListBean> list = dataBean.getList();
for (int j=0;j<list.size();j++){
if (list.get(j).getSelected()==0){
return false;
}
}
}
return true;
}
/*判断所有的子条目是否被选中*/
public boolean isChildSelected(int groupPosition) {
ProductBean.DataBean dataBean = proList.get(groupPosition);
List<ProductBean.DataBean.ListBean> list = dataBean.getList();
for (int i = 0; i < list.size(); i++) {
ProductBean.DataBean.ListBean listBean = list.get(i);
//===0没选中
if (listBean.getSelected() == 0) {
return false;
}
}
return true;
}
/*判断全选框是否选中*/
public void isAllSelected(boolean flag) {
for (int i = 0; i < proList.size(); i++) {
ProductBean.DataBean dataBean = proList.get(i);
List<ProductBean.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(flag ? 1 : 0);
}
}
EventBus.getDefault().post(TotalAll());
}
/*设置加减器*/
public void changAddSubNumber(int groupPosition, int childPosition, int num) {
List<ProductBean.DataBean.ListBean> list = proList.get(groupPosition).getList();
ProductBean.DataBean.ListBean listBean = list.get(childPosition);
listBean.setNum(num);
}
/*判断全选*/
public void isAllQuan(boolean flag) {
MsgEvent msgEvent = new MsgEvent(flag);
EventBus.getDefault().post(msgEvent);
}
private CountPrice TotalAll() {
int price = 0;
int num = 0;
int total = 1;
for (int i = 0; i < proList.size(); i++) {
ProductBean.DataBean dataBean = proList.get(i);
List<ProductBean.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
ProductBean.DataBean.ListBean listBean = list.get(j);
//选中的
if (listBean.getSelected() == 1) {
num += listBean.getNum();
price += listBean.getNum() * listBean.getPrice();
} else {
total = 0;
}
}
}
if (total == 0) {
isAllQuan(false);
} else {
isAllQuan(true);
}
CountPrice countPrice = new CountPrice();
countPrice.setCount(num);
countPrice.setPrice(price);
return countPrice;
}
//=======组--商家===========
class GroupHolder {
CheckBox group_box;
TextView group_name;
}
//======子--商品===========
class ChildHolder {
CheckBox children_box;
SimpleDraweeView children_image;
TextView children_content;
TextView children_price;
MyAddSubView children_addsub;
}
===================用不到======================
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener, IView {
private ExpandableListView main_expand;
private CheckBox main_all_box;
private TextView main_all_price;
private Button main_jiesuan_btn;
private ShopPresenter presenter;
private ShopAdapter shopAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
if (!EventBus.getDefault().isRegistered(true)) {
EventBus.getDefault().register(this);
}
}
private void initData() {
presenter = new ShopPresenter(this);
presenter.getShopData(71);
}
@Override
public void success(List<ProductBean.DataBean> proList) {
shopAdapter = new ShopAdapter(proList);
main_expand.setAdapter(shopAdapter);
//展开二级列表
for (int i = 0; i < proList.size(); i++) {
main_expand.expandGroup(i);
}
}
//赋值给总价
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onMessageEvent(CountPrice countPrice) {
//总价
main_all_price.setText(countPrice.getPrice() + "");
//数量
main_jiesuan_btn.setText("去结算(" + countPrice.getCount() + ")");
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onMessageEvent(MsgEvent msgEvent) {
main_all_box.setChecked(msgEvent.isChecked());
}
private void initView() {
main_expand = (ExpandableListView) findViewById(R.id.main_expand);
main_all_box = (CheckBox) findViewById(R.id.main_all_box);
main_all_price = (TextView) findViewById(R.id.main_all_price);
main_jiesuan_btn = (Button) findViewById(R.id.main_jiesuan_btn);
main_all_box.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_all_box:
boolean allCheckBox = shopAdapter.isAllCheckBox();
shopAdapter.isAllSelected(!allCheckBox);
shopAdapter.notifyDataSetChanged();
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().isRegistered(true);
EventBus.getDefault().unregister(this);
}
}