仿京东购物车页面

、、依赖

//ok
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
//gson
implementation 'com.google.code.gson:gson:2.8.4'
//拦截器的依赖
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
//reclerview的依赖
implementation 'com.android.support:recyclerview-v7:27.1.1'
//xRecyclerView的依赖
implementation 'com.jcodecraeer:xrecyclerview:1.3.2'
implementation 'com.android.support:design:27.1.1'
//插件依赖
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

//京东页面布局

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"
    >

<com.bway.my_demo_yue.mvp.cart.view.view.MyTitleView
    android:id="@+id/my_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
com.bway.my_demo_yue.mvp.cart.view.view.MyTitleView>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/order_msg"
        android:layout_alignParentRight="true"
        />

    <ExpandableListView
        android:id="@+id/cart_elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="60sp"
        android:layout_below="@id/my_title"
        >ExpandableListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true">

        <CheckBox
            android:id="@+id/cart_quanxuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选" />

        <TextView
            android:id="@+id/cart_he"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="合计:¥0.0" />

        <Button
            android:id="@+id/cart_buy_orders"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="去结算(0)" />

    LinearLayout>
RelativeLayout>


//加减按钮布局

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60sp"
    android:layout_height="30dp"
    android:orientation="horizontal"
    android:background="#ccc"
    android:paddingLeft="@dimen/padding_left"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:layout_marginLeft="-15dp"
    >


    <TextView
        android:id="@+id/jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:background="#fff"
        android:gravity="center"
        android:layout_weight="1"
        />
<TextView
    android:id="@+id/num"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1"
    android:layout_weight="1"
    android:background="#fff"
    android:gravity="center"
    />

    <TextView
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#fff"
        android:text="+"
        />


LinearLayout>

//二级列表父布局页面

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="60sp"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/padding_left"
    >


    <CheckBox
        android:id="@+id/seller_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/seller_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="文字"
        />

LinearLayout>

//二级列表子布局

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="120dp"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/padding_left">


    <CheckBox
        android:id="@+id/child_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/product_img"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="20dp"
        android:src="@mipmap/ic_launcher" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/product_title_name_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品标题" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/child_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="¥0.0" />

            <com.bway.my_demo_yue.activity.MySubAddView
                android:id="@+id/add_remove_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp">com.bway.my_demo_yue.activity.MySubAddView>
        LinearLayout>


    LinearLayout>


LinearLayout>

//二级列表子布局页面需要用到自定义view(自定义加减按钮)

public class MySubAddView extends LinearLayout {


    @BindView(R.id.jian)
    TextView jian;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.jia)
    TextView jia;
    int number = 1;

    public MySubAddView(Context context) {
        this(context, null);
    }

    public MySubAddView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取视图
        View view = inflate(context, R.layout.my_sub_add, this);
        //初始化
        ButterKnife.bind(view);

    }


    @OnClick({R.id.jian, R.id.num, R.id.jia})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.jian:
                if (number > 1) {
                    --number;
                    num.setText(number + "");
                    if (onNumberChangeListener != null) {
                        onNumberChangeListener.onNumberChange(number);
                    }
                } else {
                    Toast.makeText(getContext(), "商品数量最少为1", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.jia:
                    number++;
                    num.setText(number + "");
                    if (onNumberChangeListener != null) {
                        onNumberChangeListener.onNumberChange(number);
                    }

                break;
        }
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
        num.setText(number+"");
    }

    onNumberChangeListener onNumberChangeListener;

    public void setOnNumberChangeListener(MySubAddView.onNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }

    public interface onNumberChangeListener {
        void onNumberChange(int num);
    }
}

//购物车二级列表适配器

public class ExpandViewAdapter extends BaseExpandableListAdapter {

    private static final String TAG = "适配器*******";
    private List seller;

    public ExpandViewAdapter(List seller) {
        this.seller = seller;
    }

    @Override
    public int getGroupCount() {
        return seller == null ? 0 : seller.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return seller.get(groupPosition).getList().size();
    }

    @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 View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        CartBean.DataBean dataBean = seller.get(groupPosition);
        Log.d(TAG, "getGroupView: /////" + dataBean);
        ParentViewHolder parentViewHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_parent, null);
            parentViewHolder = new ParentViewHolder(convertView);
            convertView.setTag(parentViewHolder);
        } else {
            parentViewHolder = (ParentViewHolder) convertView.getTag();
        }

//赋值商家名称
        parentViewHolder.sellerNameTv.setText(dataBean.getSellerName());

        boolean b = productStatus(groupPosition);
        parentViewHolder.sellerCb.setChecked(b);

        parentViewHolder.sellerCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onSellerCheckChange(groupPosition);
                }
            }
        });
        return convertView;
    }

    //子类
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        CartBean.DataBean.ListBean listBean = seller.get(groupPosition).getList().get(childPosition);
        Log.d(TAG, "getChildView:_______ " + listBean);
        ChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_child, null);
            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        //为商品赋值
        childHolder.productTitleNameTv.setText(listBean.getTitle());
        childHolder.childPrice.setText(listBean.getPrice() + "");
        String[] pic = listBean.getImages().split("\\|");
        ImageLoader.getInstance().displayImage(pic[0], childHolder.productImg, MyApp.getOptions());

        childHolder.childCb.setChecked(listBean.getSelected() == 1);
        childHolder.childCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductCheckedChange(groupPosition, childPosition);
                }
            }
        });

        //总数量
        childHolder.addRemoveView.setNumber(listBean.getNum());
        childHolder.addRemoveView.setOnNumberChangeListener(new MySubAddView.onNumberChangeListener() {
            @Override
            public void onNumberChange(int num) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductNumberChange(groupPosition, childPosition, num);
                }
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    //商家复选框的状态
    public boolean productStatus(int groupPosition) {

        List list = seller.get(groupPosition).getList();
        for (CartBean.DataBean.ListBean listBean : list) {
            if (listBean.getSelected() == 0) {
                return false;
            }
        }
        return true;
    }

    //点击完商家按钮,子类按钮跟着改变
    public void noProductStatus(int groupPosition, boolean b) {
        List list = seller.get(groupPosition).getList();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSelected(b ? 1 : 0);
        }
    }

    //所有商品是否选中
    public boolean isAllProductSelected() {
        for (int i = 0; i < seller.size(); i++) {
            List list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    //当子类商品点击的时候被调用,改变当前商品的状态
    public void changeCurrentProdictStatus(int groupPosition, int childPosition) {

        List list = seller.get(groupPosition).getList();
        CartBean.DataBean.ListBean listBean = list.get(childPosition);

        listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
    }

    //设置当前商品的状态
    public void changeAllProductStatus(boolean selected) {
        for (int i = 0; i < seller.size(); i++) {
            List list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(selected ? 1 : 0);
            }
        }
    }

    //计算总价
    public float calcutetotalPrice() {
        float totalPrice = 0;
        for (int i = 0; i < seller.size(); i++) {
            List list = seller.get(i).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;
    }

    public int calcuteTotalNumber() {
        int totalNumber = 0;
        for (int i = 0; i < seller.size(); i++) {
            List list = seller.get(i).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 void changeCurrentNumberListener(int groupPosition, int childPosition, int number) {

        List list = seller.get(groupPosition).getList();

        CartBean.DataBean.ListBean listBean = list.get(childPosition);

        listBean.setNum(number);

    }

    //父类
    static class ParentViewHolder {
        @BindView(R.id.seller_cb)
        CheckBox sellerCb;
        @BindView(R.id.seller_name_tv)
        TextView sellerNameTv;

        ParentViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //子类
    static class ChildHolder {
        @BindView(R.id.child_cb)
        CheckBox childCb;
        @BindView(R.id.product_img)
        ImageView productImg;
        @BindView(R.id.product_title_name_tv)
        TextView productTitleNameTv;
        @BindView(R.id.child_price)
        TextView childPrice;
        @BindView(R.id.add_remove_view)
        MySubAddView addRemoveView;

        ChildHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    onCartListChangeListener onCartListChangeListener;

    public void setOnCartListChangeListener(ExpandViewAdapter.onCartListChangeListener onCartListChangeListener) {
        this.onCartListChangeListener = onCartListChangeListener;
    }

    //接口回调
    public interface onCartListChangeListener {

        void onSellerCheckChange(int groupPosition);

        void onProductCheckedChange(int groupPosition, int childPosition);

        void onProductNumberChange(int groupPosition, int childPosition, int number);
    }

}

//购物车fragment页面

public class CartFragment extends BaseFragment implements CartView, View.OnClickListener {

    private static final String TAG = "CartFragment*****";

    private ExpandableListView expandableListView;
    private ExpandViewAdapter adapter;
    private CheckBox quanxuan;
    private TextView tv_he;
    private Button btn_money;
    // private TextView zongshu;

    @Override
    protected void initData() {
        presenter.doCart();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initViews(View view) {
        expandableListView = view.findViewById(R.id.cart_elv);
        quanxuan = view.findViewById(R.id.cart_quanxuan);
        tv_he = view.findViewById(R.id.cart_he);
        btn_money = view.findViewById(R.id.cart_buy_orders);
        quanxuan.setOnClickListener(this);
    }


    @Override
    protected int provId() {
        return R.layout.cartfragment;
    }

    @Override
    protected CartPresenter provide() {
        return new CartPresenter((CartView) this);
    }


    @Override
    public void onSuccess(CartBean cartBean) {
        //创建适配器
        List data = cartBean.getData();
        adapter = new ExpandViewAdapter(data);
        expandableListView.setAdapter(adapter);

        adapter.setOnCartListChangeListener(new ExpandViewAdapter.onCartListChangeListener() {
            //关于商家
            @Override
            public void onSellerCheckChange(int groupPosition) {
                //商家点击事件
                boolean b = adapter.productStatus(groupPosition);
                //点击完商家按钮,子类按钮跟着改变
                adapter.noProductStatus(groupPosition, !b);
                //刷新适配器
                adapter.notifyDataSetChanged();
                reFreshSelectedAndToTalPriceAndTotalAllNumber();

            }

            @Override
            public void onProductCheckedChange(int groupPosition, int childPosition) {

                //子类按钮跟着改变状态
                adapter.changeCurrentProdictStatus(groupPosition, childPosition);
                //刷新适配器
                adapter.notifyDataSetChanged();
                reFreshSelectedAndToTalPriceAndTotalAllNumber();
            }

            //关于加减按钮
            @Override
            public void onProductNumberChange(int groupPosition, int childPosition, int number) {

                adapter.changeCurrentNumberListener(groupPosition, childPosition, number);

                //刷新适配器
                adapter.notifyDataSetChanged();
                reFreshSelectedAndToTalPriceAndTotalAllNumber();

            }
        });

        //展开二级列表
        for (int i = 0; i < data.size(); i++) {
            expandableListView.expandGroup(i);
        }
    }

    @Override
    public void onFaild(String error) {

    }

    private void reFreshSelectedAndToTalPriceAndTotalAllNumber() {
        //判断类型是否全部选中
        boolean allProdictSelectedd = adapter.isAllProductSelected();
//设置全选
        quanxuan.setChecked(allProdictSelectedd);
        //计算总价
        float totalPrice = adapter.calcutetotalPrice();
        tv_he.setText("总价" + totalPrice);
        //设置总数量
        int totalNumber = adapter.calcuteTotalNumber();
        btn_money.setText("合计(" + totalNumber + ")");
    }


    @Override
    public void onClick(View v) {
        //底部全选按钮
        boolean allProductSelected = adapter.isAllProductSelected();
        adapter.changeAllProductStatus(!allProductSelected);
        adapter.notifyDataSetChanged();
        reFreshSelectedAndToTalPriceAndTotalAllNumber();
    }
}

//model代码

public class CartModel {

    private static final String TAG = "CartModel-----";
    //联网请求
    public void getCart(final onCallBack onCallBack){
        String url= HttpConfig.URL_CART;
        OkHttpUtils.getInstance().doGet(url, new OkHttpUtils.onCallBack() {
            @Override
            public void onFaild(Exception e) {
                if(onCallBack!=null){
                    onCallBack.onFaild("有错");
                }
            }

            @Override
            public void onResponse(String json) {

                Gson gson = new Gson();

                CartBean cartBean = gson.fromJson(json, CartBean.class);
                String code = cartBean.getCode();
                Log.d(TAG, "onResponse: ******"+code);
                if("0".equalsIgnoreCase(code)){
                   if(onCallBack!=null){
                       onCallBack.onSuccess(cartBean);
                       Log.d(TAG, "解析mmmmmmmmmmm "+json);
                   }
                }else{
                    if(onCallBack!=null){
                        onCallBack.onFaild("有错误");
                    }
                }
            }
        });
    }
    //接口回调
    public interface onCallBack{
        void onSuccess(CartBean cartBean);
        void onFaild(String error);
    }
}

//p层代码

public class CartPresenter extends BasePresenter {

    private static final String TAG = "CartPresenter----";

    private CartModel cartModel;

    public CartPresenter(CartView view) {
        super(view);
    }


    @Override
    protected void initModel() {
        cartModel = new CartModel();
    }

    public void doCart() {
        cartModel.getCart(new CartModel.onCallBack() {
            @Override
            public void onSuccess(CartBean cartBean) {
                if (view != null) {
                    view.onSuccess(cartBean);
                    Log.d(TAG, "onSuccess:pppppppppppppppp "+cartBean);
                }
            }
            @Override
            public void onFaild(String error) {
                if (view != null) {
                    view.onFaild(error);
                }
            }
        });
    }
}

//v层代码

import com.bway.my_demo_yue.base.IView;
import com.bway.my_demo_yue.mvp.cart.mode.bean.CartBean;

public interface CartView extends IView {

    void onSuccess(CartBean cartBean);
    void onFaild(String error);
}


//购物车自定义标题栏代码

public class MyTitleView extends LinearLayout {



    public MyTitleView(Context context) {
        super(context);
    }

    public MyTitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyTitleView);
        String title = array.getString(R.styleable.MyTitleView_title_tv);
        String tv_write = array.getString(R.styleable.MyTitleView_tv_write);
        View view = inflate(context, R.layout.title_text, this);
        //获取id
        TextView tv_tle=view.findViewById(R.id.title_tv);
        TextView tv_wri= view.findViewById(R.id.tv_write);
    }
}

//布局

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="60sp"
    android:orientation="horizontal"
    >

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="购物车"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="30dp"
        />


<TextView
    android:id="@+id/tv_write"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="编辑"
    android:layout_weight="1"
    android:gravity="center"
    />



LinearLayout>

 
xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTitleView">
        <attr name="title_tv" format="string">attr>
        <attr name="tv_write" format="string">attr>

    declare-styleable>

resources>




你可能感兴趣的:(仿京东购物车页面)