布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="10dp"
android:id="@+id/btnBack"
android:text="返回"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物车"
android:textSize="25sp"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:padding="10dp"
android:textSize="25sp"
android:id="@+id/btnEditor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编辑"/>
</LinearLayout>
<ExpandableListView
android:id="@+id/exlv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff">
</ExpandableListView>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/qx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/checkbox"
android:text="全选" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/qx"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总价:"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="16sp"
android:text="0.00" />
<TextView
android:layout_marginLeft="30dp"
android:id="@+id/num"
android:textColor="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="共0件商品" />
</LinearLayout>
<Button
android:id="@+id/jiesuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#ff0000"
android:text="去结算(0)" />
</RelativeLayout>
</LinearLayout>
group
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_marginTop="20dp"
android:id="@+id/che"
android:layout_width="25dp"
android:layout_height="25dp" />
<TextView
android:layout_marginTop="20dp"
android:id="@+id/g_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="------------" />
</LinearLayout>
chidren
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<CheckBox
android:layout_marginLeft="20dp"
android:id="@+id/z_che"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="11dp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/z_che"
android:orientation="vertical">
<TextView
android:id="@+id/z_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="三只松鼠大礼包"
android:textSize="16sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/z_img"
android:layout_width="140dp"
android:layout_height="140dp"
app:fadeDuration="20"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/z_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/z_img"
android:layout_toRightOf="@+id/z_img"
android:layout_marginLeft="5dp"
android:text="价钱"
android:textColor="#ff0000" />
<TextView
android:id="@+id/z_shuxing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/z_price"
android:layout_toEndOf="@+id/z_img"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/z_img"
android:layout_marginTop="5dp"
android:textSize="14sp"
android:textColor="@color/colorPrimary"
android:text="价钱" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/z_shuxing"
android:layout_marginLeft="5dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/z_img"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_jian"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/shopcart_minus_red" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:text="1" />
<ImageView
android:id="@+id/iv_add"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:src="@drawable/shopcart_add_red" />
<ImageView
android:id="@+id/del"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:layout_weight="1"
android:src="@drawable/rublish" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
接口
public interface ApiService {
//注册接口http://120.27.23.105/user/reg
@GET("user/reg")
Flowable<RegisteBean> getRegist(@QueryMap Map<String,String> map);
//登录接口http://120.27.23.105/user/login
@GET("user/login")
Flowable<LoginBean> getLogin(@QueryMap Map<String,String> map);
//商品列表https://www.zhaoapi.cn/product/getProducts?pscid=39&page=1
@GET("product/getProducts")
Flowable<GoodsListBean> getGoodsList(@QueryMap Map<String,String> map);
//商品详情https://www.zhaoapi.cn/product/getProductDetail
@GET("product/getProductDetail")
Flowable<GoodsDetailBean> getGoodsDetail(@QueryMap Map<String,String> map);
//添加购物车http://120.27.23.105/product/addCart
@GET("product/addCart")
Flowable<AddBean> getAdd(@QueryMap Map<String,String> map);
//查询购物车http://120.27.23.105/product/getCarts
@GET("product/getCarts")
Flowable<SelectBean> getSelectCart(@QueryMap Map<String,String> map);
//删除购物车http://120.27.23.105/product/deleteCart?uid=72&pid=1
@GET("product/deleteCart")
Flowable<DelBean> getDelCart(@QueryMap Map<String,String> map);
//创建订单http://120.27.23.105/product/createOrder?uid=71&price=99.99
@GET("product/createOrder")
Flowable<DelBean> getCjOrder(@QueryMap Map<String,String> map);
//订单列表http://120.27.23.105/product/getOrders?uid=71
@GET("product/getOrders")
Flowable<DingdanBean> getOrderList(@QueryMap Map<String,String> map);
//修改订单状态http://120.27.23.105/product/updateOrder?uid=71&status=1&orderId=1
@GET("product/updateOrder")
Flowable<UpdateBean> getUpDateOrder(@QueryMap Map<String,String> map);
}
public class ShoppingCartActivity extends AppCompatActivity implements IView, CjOrderView {
private static final String TAG = "ShoppingCartActivity";
@BindView(R.id.btnBack)
TextView btnBack;
@BindView(R.id.btnEditor)
TextView btnEditor;
@BindView(R.id.exlv)
ExpandableListView exlv;
@BindView(R.id.checkbox)
CheckBox checkbox;
@BindView(R.id.qx)
TextView qx;
@BindView(R.id.price)
TextView price;
@BindView(R.id.num)
TextView num;
@BindView(R.id.jiesuan)
Button jiesuan;
@BindView(R.id.rl)
RelativeLayout rl;
private ArrayList<SelectBean.DataBean> groupList = new ArrayList<>();
private ArrayList<List<SelectBean.DataBean.ListBean>> childList = new ArrayList<>();
private boolean flag;
private SelectCartPresenter presenter;
private MyCarShowAdapter myCarShowAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
setContentView(R.layout.activity_shopping_cart);
ButterKnife.bind(this);
//初始化数据
initData();
//点击隐藏
if (childList.size() >= 0) {
btnEditor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!flag) {// false 隐藏 true 代表显示
//隐藏
flag = true;
myCarShowAdapter.showDelete(flag);
btnEditor.setText("完成");
} else {
//显示
flag = false;
myCarShowAdapter.showDelete(flag);
btnEditor.setText("编辑");
}
}
});
}
}
private void cjOrder(){
CjOrderPresenter cjOrderPresenter = new CjOrderPresenter();
cjOrderPresenter.attachView(this);
HashMap<String, String> map = new HashMap<>();
map.put("source", "android");
map.put("uid","4043");
map.put("price",price.getText()+"");
Log.e(TAG, "cjOrder: "+price.getText() );
cjOrderPresenter.getData(map,"http://120.27.23.105/");
}
private void initData() {
// sp = App.sp;
// boolean b = sp.getBoolean("b", false);
//查询购物车
presenter = new SelectCartPresenter();
presenter.attachView(this);
HashMap<String, String> map = new HashMap<>();
map.put("source", "android");
map.put("uid", "4043");
// Log.e(TAG, "initData: " + sp.getString("uid", "") + b);
presenter.getData(map, "http://120.27.23.105/");
}
@OnClick({R.id.btnBack, R.id.btnEditor, R.id.checkbox, R.id.jiesuan})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btnBack:
finish();
break;
case R.id.btnEditor:
break;
case R.id.checkbox:
myCarShowAdapter.changeAllListCbState(checkbox.isChecked());
break;
case R.id.jiesuan:
Toast.makeText(ShoppingCartActivity.this,"点击了结算",Toast.LENGTH_SHORT).show();
if (price.getText().equals("0.00") || price.getText().equals("0.0")) {
Toast.makeText(ShoppingCartActivity.this, "价格不能为空", Toast.LENGTH_SHORT).show();
} else {
cjOrder();
}
break;
}
}
@Override
public void onSuccessed(Object o) {
SelectBean selectBean = (SelectBean) o;
List<SelectBean.DataBean> data = selectBean.getData();
Log.e(TAG, "onSuccessedsssssssss: " + data.toString());
groupList.addAll(data);
for (int i = 0; i < data.size(); i++) {
List<SelectBean.DataBean.ListBean> list = data.get(i).getList();
childList.add(list);
}
myCarShowAdapter = new MyCarShowAdapter(ShoppingCartActivity.this, groupList, childList);
exlv.setAdapter(myCarShowAdapter);
exlv.setGroupIndicator(null);
// 展开
for (int i = 0; i < groupList.size(); i++) {
exlv.expandGroup(i);
}
}
@Override
public void onFailed(Exception e) {
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detachView();
}
EventBus.getDefault().unregister(this);
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
checkbox.setChecked(event.isCheckd());
}
@Subscribe
public void onMessageEven(PriceAndCountEvent event) {
price.setText(event.getPrice() + "");
num.setText("共" + event.getCount() + "件商品");
jiesuan.setText("去结算(" + event.getTo() + ")");
}
@Override
public void cjSuccessed(Object o) {
DelBean delBean= (DelBean) o;
String msg = delBean.getMsg();
Toast.makeText(ShoppingCartActivity.this,msg+"",Toast.LENGTH_SHORT).show();
if (delBean.getCode().equals("0")){
Intent intent = new Intent(ShoppingCartActivity.this, OrderActivity.class);
// intent.putExtra("getprice", price.getText() + "");
startActivity(intent);
}
}
@Override
public void cjFailed(Exception e) {
Log.e(TAG, "cjFailed: "+e.getMessage() );
}
}
public class MyCarShowAdapter extends BaseExpandableListAdapter implements DelView {
private Context context;
private ArrayList<SelectBean.DataBean> data;
private ArrayList<List<SelectBean.DataBean.ListBean>> list;
private boolean isVisible;
private SharedPreferences myself;
private DelPresenter presenter;
public MyCarShowAdapter(Context context, ArrayList<SelectBean.DataBean> data, ArrayList<List<SelectBean.DataBean.ListBean>> list) {
this.context = context;
this.data = data;
this.list = list;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//一级标题
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.group1, null);
holder.textView = (TextView) convertView.findViewById(R.id.g_title);
holder.che = (CheckBox) convertView.findViewById(R.id.che);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(data.get(groupPosition).getSellerName());
//设置一级列表checkBox的状态
holder.che.setChecked(data.get(groupPosition).isChecked());
//一级列表checkBox的点击事件
holder.che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断一级列表复选框的状态 设置为true或false
data.get(groupPosition).setChecked(holder.che.isChecked());
//改变二级checkbod的状态
changeChildCbState(groupPosition, holder.che.isChecked());
//算钱
EventBus.getDefault().post(computer());
//改变全选状态 isAllGroupCbSelect判断一级是否全部选中
changeAllCbState(isAllGroupCbSelect());
//必刷新
notifyDataSetChanged();
}
});
return convertView;
}
//二级标题
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Fresco.initialize(context);
final SelectBean.DataBean.ListBean listBean = list.get(groupPosition).get(childPosition);
final ViewHolder2 holder2;
if (convertView == null) {
holder2 = new ViewHolder2();
convertView = View.inflate(context, R.layout.chidren, null);
holder2.z_title = (TextView) convertView.findViewById(R.id.z_title);
holder2.z_che = (CheckBox) convertView.findViewById(R.id.z_che);
holder2.img = (SimpleDraweeView) convertView.findViewById(R.id.z_img);
holder2.price = (TextView) convertView.findViewById(R.id.z_price);
holder2.xiangqing = (TextView) convertView.findViewById(R.id.z_shuxing);
holder2.jian = (ImageView) convertView.findViewById(R.id.iv_jian);
holder2.jia = (ImageView) convertView.findViewById(R.id.iv_add);
holder2.del = (ImageView) convertView.findViewById(R.id.del);
holder2.num = (TextView) convertView.findViewById(R.id.tv_num);
convertView.setTag(holder2);
} else {
holder2 = (ViewHolder2) convertView.getTag();
}
holder2.num.setText(list.get(groupPosition).get(childPosition).getNum() + "");
holder2.z_title.setText(list.get(groupPosition).get(childPosition).getTitle());
holder2.price.setText("¥" + list.get(groupPosition).get(childPosition).getBargainPrice());
holder2.xiangqing.setText(list.get(groupPosition).get(childPosition).getSubhead());
String images = list.get(groupPosition).get(childPosition).getImages();
String[] split = images.split("\\|");
// ImageLoader.getInstance().displayImage(split[0], holder2.img);
Uri parse = Uri.parse(split[0]);
holder2.img.setImageURI(parse);
//判断是否需要显示删除按钮
if (!isVisible) {
//隐藏
holder2.del.setVisibility(View.GONE);
} else {
holder2.del.setVisibility(View.VISIBLE);
}
//设置二级列表checkbox的属性
holder2.z_che.setChecked(list.get(groupPosition).get(childPosition).isChecked());
//二级列表的点击事件
holder2.z_che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置该条目中的checkbox属性值
listBean.setChecked(holder2.z_che.isChecked());
//计算价钱
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
//判断当前checkbox是选中的状态
if (holder2.z_che.isChecked()) {
//如果全部选中(isAllChildCbSelected)
if (isAllChildCbSelected(groupPosition)) {
//改变一级列表的状态
changeGroupCbState(groupPosition, true);
//改变全选的状态
changeAllCbState(isAllGroupCbSelect());
}
} else {
//如果没有全部选中,一级列表的checkbox为false不为选中
changeGroupCbState(groupPosition, false);
changeAllCbState(isAllGroupCbSelect());
}
notifyDataSetChanged();
}
});
//加号
holder2.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
//num为int类型所以要加空字符串
holder2.num.setText(++num + "");
listBean.setNum(num);
//如果二级列表的checkbox为选中,计算价钱
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//减号
holder2.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
if (num == 1) {
Toast.makeText(context, "不能减了", Toast.LENGTH_SHORT).show();
return;
}
holder2.num.setText(--num + "");
listBean.setNum(num);
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
// final String uid = App.sp.getString("uid", null);
holder2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
presenter = new DelPresenter();
presenter.attachView(MyCarShowAdapter.this);
HashMap<String, String> map = new HashMap<>();
map.put("source","android");
map.put("uid","4043");
map.put("pid",list.get(groupPosition).get(childPosition).getPid()+"");
presenter.getData(map,"http://120.27.23.105/");
List<SelectBean.DataBean.ListBean> listBeen = list.get(groupPosition);
SelectBean.DataBean.ListBean remove = listBeen.remove(childPosition);
if (listBeen.size()==0){
list.remove(groupPosition);
data.remove(groupPosition);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
});
// myself = context.getSharedPreferences("myself", Context.MODE_PRIVATE);
// uid = myself.getString("uid", null);
//点击删除购物车里面的内容
/* holder2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// if (holder2.z_che.isChecked()) {
//点击传值
car_select_presenter.delCar(uid,list.get(groupPosition).get(childPosition).getPid() + "");
List<Car_Select.DataBean.ListBean> listBeen = list.get(groupPosition);
Car_Select.DataBean.ListBean remove = listBeen.remove(childPosition);
if (listBeen.size() == 0) {
//先移除二级列表的集合,再移除一级列表的集合
list.remove(groupPosition);
data.remove(groupPosition);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
*//* } else {
Toast.makeText(context, "请选择商品", Toast.LENGTH_SHORT).show();
}*//*
}
});*/
return convertView;
}
/**
* 删除返回方法
* @param delBean
*/
@Override
public void onDelSuccessed(DelBean delBean) {
Toast.makeText(context, delBean.getMsg(), Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
@Override
public void onDelFailed(Exception e) {
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class ViewHolder {
TextView textView;
CheckBox che;
}
class ViewHolder2 {
SimpleDraweeView img;
TextView z_title;
CheckBox z_che;
TextView price;
TextView xiangqing;
ImageView jia;
ImageView jian;
ImageView del;
TextView num;
}
//改变二级列表的checkbox的状态 如果一级选中,控制二级也选中
private void changeChildCbState(int groupPosition, boolean flag) {
List<SelectBean.DataBean.ListBean> listBeen = list.get(groupPosition);
for (int j = 0; j < listBeen.size(); j++) {
SelectBean.DataBean.ListBean listBean = listBeen.get(j);
listBean.setChecked(flag);
}
}
//判断一级列表是否全部选中
public boolean isAllGroupCbSelect() {
for (int i = 0; i < list.size(); i++) {
SelectBean.DataBean dataBean = data.get(i);
if (!dataBean.isChecked()) {
return false;
}
}
return true;
}
//改变全选的状态
private void changeAllCbState(boolean flag) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheckd(flag);
EventBus.getDefault().post(messageEvent);
}
//改变一级列表的checkbox的状态
private void changeGroupCbState(int i, boolean flag) {
SelectBean.DataBean dataBean = data.get(i);
dataBean.setChecked(flag);
}
//判断二级列表是否全部选中
private boolean isAllChildCbSelected(int i) {
List<SelectBean.DataBean.ListBean> listBeen = list.get(i);
for (int j = 0; j < listBeen.size(); j++) {
SelectBean.DataBean.ListBean listBean = listBeen.get(j);
if (!listBean.isChecked()) {
return false;
}
}
return true;
}
//设置全选,反选
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < list.size(); i++) {
changeGroupCbState(i, flag);
changeChildCbState(i, flag);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
//计算列表的价钱
private PriceAndCountEvent computer() {
int count = 0;
double price = 0;
int to = 0;
for (int i = 0; i < list.size(); i++) {
List<SelectBean.DataBean.ListBean> listBeen = list.get(i);
for (int j = 0; j < listBeen.size(); j++) {
SelectBean.DataBean.ListBean listBean = listBeen.get(j);
if (listBean.isChecked()) {
price += listBean.getNum() * listBean.getBargainPrice();
count += listBean.getNum();
to += listBean.getNum();
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
priceAndCountEvent.setTo(to);
return priceAndCountEvent;
}
/**
* 是否隐藏删除按钮
*
* @param isVisible
* @return
*/
public void showDelete(boolean isVisible) {
this.isVisible = isVisible;
notifyDataSetChanged();
}
}