购物车
布局
主布局
RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
数量加减布局
LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:padding=“2dp”
android:layout_marginLeft=“10dp”
android:layout_width=“60dp”
android:layout_height=“30dp”
android:layout_gravity=“center_vertical”
android:background="#99000000"
android:gravity=“center_vertical”>
ExpandableListView父布局
ExpandableListView子布局
LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“120dp”
android:gravity=“center_vertical”>
MainActivity
package com.www.shopcar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ExpandableListView expand_menu;
private CheckBox cb_cart_all_select;
private TextView tv_cart_total_price;
private Button btn_cart_pay;
String url = "http://www.zhaoapi.cn/product/getCarts";
private ShopCarAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
HashMap map = new HashMap<>();
map.put("uid","71");
OkHttpUtils.getInstance().doPost(url, map, new OkHttpUtils.OkCallback() {
@Override
public void onFile(Exception e) {
}
@Override
public void onSuccess(String json) {
ShopInfo shopInfo = new Gson().fromJson(json, ShopInfo.class);
if("0".equals(shopInfo.getCode())){
List data = shopInfo.getData();
adapter = new ShopCarAdapter(data);
adapter.setOnCartListChangeListener(new ShopCarAdapter.onCartListChangeListener() {
@Override
public void onSellerCheckedChange(int groupPosition) {
boolean currentSellerAllProductSelected = adapter.isCurrentSellerAllProductSelected(groupPosition);
adapter.changeCurrentSellerAllProductsStatus(groupPosition, !currentSellerAllProductSelected);
adapter.notifyDataSetChanged();
//B.刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductCheckedChange(int groupPosition, int childPosition) {
//点击商品得checkbox
adapter.changeCurrentProductStatus(groupPosition,childPosition);
adapter.notifyDataSetChanged();
//B.刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
@Override
public void onProductNumberChange(int groupPosition, int childPosition, int number) {
//当加减被点击
adapter.changeCurrentProductNumber(groupPosition,childPosition,number);
adapter.notifyDataSetChanged();
//B.刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
});
expand_menu.setAdapter(adapter);
for(int x=0; x
}
封装OKhttp
package com.www.shopcar;
import android.os.Handler;
import android.os.Looper;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpUtils {
private final Handler mHandler;
private final OkHttpClient mOkHttpClient;
private static OkHttpUtils mOkHttpUtils;
private OkHttpUtils(){
mHandler = new Handler(Looper.getMainLooper());
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build();
}
public static OkHttpUtils getInstance(){
if(mOkHttpUtils==null){
synchronized (OkHttpUtils.class){
if(mOkHttpUtils==null){
return mOkHttpUtils = new OkHttpUtils();
}
}
}
return mOkHttpUtils;
}
public interface OkCallback{
void onFile(Exception e);
void onSuccess(String json);
}
public void doGet(String url, final OkCallback okCallback){
final Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if(okCallback!=null){
mHandler.post(new Runnable() {
@Override
public void run() {
okCallback.onFile(e);
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response!=null&&response.isSuccessful()){
final String json = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
if(okCallback!=null){
okCallback.onSuccess(json);
return;
}
}
});
}
}
});
}
public void doPost(String url, Map map, final OkCallback okCallback) {
FormBody.Builder builder = new FormBody.Builder();
if (map != null) {
for (String key : map.keySet()) {
builder.add(key, map.get(key));
}
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.post(formBody)
.url(url)
.build();
final Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (okCallback != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
okCallback.onFile(e);
}
});
}
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (response != null && response.isSuccessful()) {
final String json = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
if (okCallback != null) {
okCallback.onSuccess(json);
}
}
});
}
}
});
}
}
加减的自定义控件
package com.www.shopcar;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class AddSubView extends LinearLayout implements View.OnClickListener {
private TextView sub_tv;
private TextView product_number_tv;
private TextView add_tv;
private int number=1;
public AddSubView(Context context) {
this(context, null);
}
public AddSubView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AddSubView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = View.inflate(context, R.layout.addsubview, this);
sub_tv=view.findViewById(R.id.sub_tv);
product_number_tv=view.findViewById(R.id.product_number_tv);
add_tv=view.findViewById(R.id.add_tv);
sub_tv.setOnClickListener(this);
add_tv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sub_tv:
if(number>1){
--number;
product_number_tv.setText(number+"");
if(onNumberChangeListener!=null){
onNumberChangeListener.onNumberChange(number);
}
}else{
Toast.makeText(getContext(), "不能再减了", Toast.LENGTH_SHORT).show();
}
break;
case R.id.add_tv:
++number;
product_number_tv.setText(number+"");
if(onNumberChangeListener!=null){
onNumberChangeListener.onNumberChange(number);
}
break;
}
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
product_number_tv.setText(number+"");
}
public interface OnNumberChangeListener{
void onNumberChange(int num);
}
private OnNumberChangeListener onNumberChangeListener;
public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
this.onNumberChangeListener = onNumberChangeListener;
}
}
Adapter
package com.www.shopcar;
import android.content.Context;
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 java.util.List;
class ShopCarAdapter extends BaseExpandableListAdapter {
private List
public ShopCarAdapter(List shopCarBean) {
mShopCarBean=shopCarBean;
}
@Override
public int getGroupCount() {
return mShopCarBean==null ? 0 : mShopCarBean.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mShopCarBean.get(groupPosition).getList()==null ? 0: mShopCarBean.get(groupPosition).getList().size();
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ShopInfo.DataBean dataBean = mShopCarBean.get(groupPosition);
ParentViewHolder parentViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.group_item, null);
parentViewHolder = new ParentViewHolder(convertView);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
parentViewHolder.seller_name_tv.setText(dataBean.getSellerName());
boolean currentSellerAllProductSelected = isCurrentSellerAllProductSelected(groupPosition);
//更新UI
parentViewHolder.seller_cb.setChecked(currentSellerAllProductSelected);
parentViewHolder.seller_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击商家得checkBox
if (onCartListChangeListener != null) {
onCartListChangeListener.onSellerCheckedChange(groupPosition);
}
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ShopInfo.DataBean dataBean = mShopCarBean.get(groupPosition);
List list = dataBean.getList();
ShopInfo.DataBean.ListBean listBean = list.get(childPosition);
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.child_item, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
} else {
childViewHolder=(ChildViewHolder)convertView.getTag();
}
childViewHolder.product_title_name_tv.setText(listBean.getTitle());
childViewHolder.product_price_tv.setText(listBean.getPrice()+"");
childViewHolder.child_cb.setChecked(listBean.getSelected() == 1);
childViewHolder.add_remove_view.setNumber(listBean.getNum());
String image = listBean.getImages();
String[] split = image.split("\\|");
String s = split[0];
Glide.with(convertView).load(s).into(childViewHolder.product_icon_iv);
childViewHolder.child_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击商品得checkBox
if (onCartListChangeListener != null) {
onCartListChangeListener.onProductCheckedChange(groupPosition, childPosition);
}
}
});
childViewHolder.add_remove_view.setOnNumberChangeListener(new AddSubView.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
//拿到商品最新得数量
if (onCartListChangeListener != null) {
onCartListChangeListener.onProductNumberChange(groupPosition, childPosition, num);
}
}
});
return convertView;
}
//当前商家所有商品是否被选中
public boolean isCurrentSellerAllProductSelected(int groupPosition) {
ShopInfo.DataBean dataBean = mShopCarBean.get(groupPosition);
//拿到商家所有的商品
List list = dataBean.getList();
//Bean类中有一个selected来判断是否选中,未选中是0
for (ShopInfo.DataBean.ListBean listBean : list) {
//只要有一个未选中,商家就直接未选中
if (listBean.getSelected() == 0) {
return false;
}
}
return true;
}
//所有商品是否被选中,双重for循环,里面的集合套集合取数据
public boolean isAllProductsSelected() {
for (int i = 0; i < mShopCarBean.size(); i++) {
ShopInfo.DataBean dataBean = mShopCarBean.get(i);
List list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 0) {
return false;
}
}
}
return true;
}
//计算商品总的数量
public int calculateTotalNumber() {
int totalNumber = 0;
for (int i = 0; i < mShopCarBean.size(); i++) {
ShopInfo.DataBean dataBean = mShopCarBean.get(i);
List list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
//只要是选中状态
if (list.get(j).getSelected() == 1) {
int num = list.get(j).getNum();
totalNumber += num;
}
}
}
return totalNumber;
}
/**
*计算总价
*/
public float calculateTotalPrice() {
float totalPrice = 0;
for (int i = 0; i < mShopCarBean.size(); i++) {
ShopInfo.DataBean dataBean = mShopCarBean.get(i);
List list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
//只要是选中状态
if (list.get(j).getSelected() == 1) {
float price = list.get(j).getPrice();
int num = list.get(j).getNum();
totalPrice += price * num;
}
}
}
return totalPrice;
}
//当商家得checkbox被点击得时候调用,设置当前商家得所有商品得状态--------商家组其所有的商品
public void changeCurrentSellerAllProductsStatus(int groupPosition, boolean isSelected) {
ShopInfo.DataBean dataBean = mShopCarBean.get(groupPosition);
List listBeans = dataBean.getList();
for (int i = 0; i < listBeans.size(); i++) {
ShopInfo.DataBean.ListBean listBean = listBeans.get(i);
listBean.setSelected(isSelected ? 1 : 0);
}
}
//当商品得checkbox被点击得时候调用,改变当前商品状态---------商品子条目
public void changeCurrentProductStatus(int groupPosition, int childPosition) {
ShopInfo.DataBean dataBean = mShopCarBean.get(groupPosition);
List listBeans = dataBean.getList();
ShopInfo.DataBean.ListBean listBean = listBeans.get(childPosition);
listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
}
//设置所有商品得状态
public void changeAllProductsStatus(boolean selected) {
for (int i = 0; i < mShopCarBean.size(); i++) {
ShopInfo.DataBean dataBean = mShopCarBean.get(i);
List list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(selected?1:0);
}
}
}
//当加减器被点击得时候调用,改变当前商品得数量
public void changeCurrentProductNumber(int groupPosition, int childPosition, int number) {
ShopInfo.DataBean dataBean = mShopCarBean.get(groupPosition);
List listBeans = dataBean.getList();
ShopInfo.DataBean.ListBean listBean = listBeans.get(childPosition);
listBean.setNum(number);
}
onCartListChangeListener onCartListChangeListener;
public void setOnCartListChangeListener(ShopCarAdapter.onCartListChangeListener onCartListChangeListener) {
this.onCartListChangeListener = onCartListChangeListener;
}
public interface onCartListChangeListener {
void onSellerCheckedChange(int groupPosition);
void onProductCheckedChange(int groupPosition, int childPosition);
void onProductNumberChange(int groupPosition, int childPosition, int number);
}
@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;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class ParentViewHolder {
public CheckBox seller_cb;
public TextView seller_name_tv;
public ParentViewHolder(View rootView) {
seller_cb = rootView.findViewById(R.id.seller_cb);
seller_name_tv = rootView.findViewById(R.id.seller_name_tv);
}
}
class ChildViewHolder {
public CheckBox child_cb;
public ImageView product_icon_iv;
public TextView product_title_name_tv;
public TextView product_price_tv;
public AddSubView add_remove_view;
public ChildViewHolder(View rootView) {
child_cb = rootView.findViewById(R.id.child_cb);
product_icon_iv = rootView.findViewById(R.id.product_icon_iv);
product_title_name_tv = rootView.findViewById(R.id.product_title_name_tv);
product_price_tv = rootView.findViewById(R.id.product_price_tv);
add_remove_view = rootView.findViewById(R.id.add_remove_view);
}
}
}