购物车

1.先定义bean类

 (展示两个数据)

public class Seller {
    public String sellerName;
    public int sellerid;
    public List<Product> list;
    public boolean isCheck;
}
public class Product {

    public String bargainPrice;
    public String createtime;
    public String detailUrl;
    public String images;
    public int num;
    public int pid;
    /**
     * 只能整型 字符串
     */
    public String price;
    public int pscid;
    public int selected;
    public int sellerid;
    public String subhead;
    public String title;
}
public class CartData {
    public String msg;
    public String code;
    public List<Seller> data;
}

2》定义接口

public interface ITask {

    void getCarts(String url, OkCallback callback);

}

3》创建类实现接口

public class Task implements ITask{

    @Override
    public void getCarts(String url, OkCallback callback) {
        OK.getOk().doGet(url,callback);
    }
}

2.Presenter

1》定义接口

public interface IPresenter extends BasePre{

    void getCarts();
}

2》实现接口

public class MyPresenter implements IPresenter {
    ITask task;
    IView view;

    public MyPresenter(ShoppingCarActivity activity){
        task=new Task();
        view=activity;
    }


    @Override
    public void getCarts() {

        task.getCarts("http://www.zhaoapi.cn/product/getCarts?uid=71", new OkCallback() {
            @Override
            public void onUI(String json) {
                Gson gson=new Gson();
                CartData cartData = gson.fromJson(json, CartData.class);
                view.showCart(cartData.data);
            }

            @Override
            public void onFailed(String json) {

            }
        });
    }

    @Override
    public void onDestroy() {
        //销毁视图
        view=null;
    }

}

3.View

1》定义接口

public interface IView  {

    void showCart(List<Seller> sellers);
}

2》适配器

public class CartAdapter extends BaseExpandableListAdapter {
    List<Seller> sellers;//商家列表
    Context context;
    LayoutInflater inflater;//初始化视图类
    ImageView  img_check_all;//全选状态展开
    boolean isCheckAll=false;//全选状态标记
    TextView txt_total_price;//合计总价展示控件

    public CartAdapter(Context context) {
        this.sellers =new ArrayList<>();
        this.inflater= LayoutInflater.from(context);
        this.context = context;
    }
   //添加数据
    public void addData(List<Seller> list) {
        sellers.addAll(list);//添加数据
        notifyDataSetChanged();//刷新数据
    }

    /**
     * 绑定顶部的总计价格控件
     *
     * @param txt_total_price
     */
    public void setTotalPrice(TextView txt_total_price) {
        this.txt_total_price = txt_total_price;
    }

    /**
     * 绑定全选按钮的控件
     *
     * @param img_check_all
     */
    public void setCheckAll(ImageView img_check_all) {
        this.img_check_all = img_check_all;
    }


    /**
     * 计算所选产品价格,只要改变购物车,价格重新计算
     */
    private void sumPrice() {
        int total = 0;//计算总价,最后赋值给控件

        for (Seller seller : sellers) {//遍历商家
            if (seller.isCheck) {//选中的店铺
                for (Product product : seller.list) {
                    if (product.selected == 1) {//选中的产品的价格计算
                        //解析价格
                        int price = (int) Double.parseDouble(product.price);
                        //累加价格
                        total = total + (price * product.num);
                    }
                }
            }
        }

        //把最终结果赋值给控件
        txt_total_price.setText("合计¥" + total);
    }

    /**
     * 全选方案
     */
    public void checkAll() {
        for (Seller seller : sellers) {//遍历商家
            seller.isCheck = true;//设置选中
            for (Product product : seller.list) {//遍历产品
                product.selected = 1;//设置选中
            }
        }

        //重新甲酸商品价格
        sumPrice();

        //刷新页面
        notifyDataSetChanged();

    }

    /**
     * 默认全部展开
     *
     * @param listView
     */
    public void expandGroup(ExpandableListView listView) {
        for (int i = 0; i < getGroupCount(); i++) {
            listView.expandGroup(i);
        }
    }
    /**
     * 获取商家数量
     *
     * @return
     */
    @Override
    public int getGroupCount() {
        return sellers.size();
    }
    /**
     * 商家下面有几个产品
     *
     * @param i
     * @return
     */
    @Override
    public int getChildrenCount(int i) {
        return sellers.get(i).list.size();
    }

    /**
     * 获取商家类
     *
     * @param i
     * @return
     */
    @Override
    public Seller getGroup(int i) {
        return sellers.get(i);
    }
    /**
     * 获取产品
     *
     * @param i
     * @param i1
     * @return
     */
    @Override
    public Product getChild(int i, int i1) {
        return sellers.get(i).list.get(i1);
    }
    /**
     * 固定商家ID
     *
     * @param i
     * @return
     */
    @Override
    public long getGroupId(int i) {
        Seller seller=getGroup(i);
        return seller.sellerid;
    }
    /**
     * 获取商品ID
     *
     * @param i
     * @param i1
     * @return
     */
    @Override
    public long getChildId(int i, int i1) {
        Product product=getChild(i,i1);
        return product.pid;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        SellerViewHolder holder;
        if (view==null){
            view=inflater.inflate(R.layout.item_store,viewGroup,false);
            holder=new SellerViewHolder();
            holder.store_checked=view.findViewById(R.id.store_checked);
            holder.store_name=view.findViewById(R.id.store_name);
            view.setTag(holder);
        }else {
            holder= (SellerViewHolder) view.getTag();
        }
        Seller seller=getGroup(i);
        holder.store_name.setText(seller.sellerName);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ProductViewHolder holder;
        if (view==null){
            view=inflater.inflate(R.layout.item_product,viewGroup,false);
            holder=new ProductViewHolder();
            holder.product_checked=view.findViewById(R.id.product_checked);
            holder.product_img=view.findViewById(R.id.product_img);
            holder.product_title=view.findViewById(R.id.product_title);
            holder.product_price=view.findViewById(R.id.product_price);
            view.setTag(holder);
        }else {
            holder= (ProductViewHolder) view.getTag();
        }
        Product product=getChild(i,i1);
        holder.product_title.setText(product.title);
        holder.product_price.setText(product.price);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    class SellerViewHolder{
        ImageView store_checked;
        TextView store_name;
    }

    class ProductViewHolder{
        ImageView product_checked,product_img;
        TextView product_title,product_price;
    }
}

4.主xml

<RelativeLayout
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<ImageView
    android:id="@+id/image_fan"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/textView"
    android:layout_marginStart="12dp"
    android:src="@drawable/fanhui" />

<TextView
    android:id="@+id/shopping_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="5dp"
    android:text="购物车"
    android:textSize="22dp"
    />

<ExpandableListView
    android:id="@+id/expandable_listview"
    android:layout_width="match_parent"
    android:layout_height="530dp"
    android:layout_below="@id/shopping_title"
   ></ExpandableListView>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/relative2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

    <ImageView
        android:id="@+id/query_checked"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:src="@drawable/ic_checked" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="30dp"
        android:text="全选" />

    <TextView
        android:id="@+id/txt_total_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="textview"
        android:textSize="18dp" />
</RelativeLayout>

商家布局页面

<ImageView
    android:id="@+id/store_checked"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="2dp"
    android:layout_alignParentStart="true"
    android:src="@drawable/ic_checked" />
<TextView
    android:id="@+id/store_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="textview"
    android:textSize="18dp"
    android:textColor="#000"
   android:layout_marginTop="8dp"
    android:layout_marginLeft="30dp"
    />

商品布局文件

<ImageView
    android:id="@+id/product_checked"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="43dp"
    android:layout_marginLeft="25dp"
    android:src="@drawable/ic_checked" />

<ImageView
    android:id="@+id/product_img"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="58dp"
    android:layout_marginTop="13dp"
    android:src="@drawable/ic_launcher_background" />

<TextView
    android:id="@+id/product_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/product_img"
    android:layout_marginStart="147dp"
    android:lines="2"
    android:text="TextView"
    android:textSize="18dp"
    android:textColor="#000"
    />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/product_title"
    android:layout_below="@+id/product_checked"
    android:lines="2"
    android:text="¥:"
    android:textColor="#FF0000"
    android:textSize="20dp" />

<TextView
    android:id="@+id/product_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/product_img"
    android:layout_toEndOf="@+id/textView4"
    android:text="TextView"
    android:textColor="#FF0000"
    android:textSize="20dp"
    />
<View.SumAddView
    android:id="@+id/add_sum"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="8dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

5.自定义view

1》布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/reduce"
        android:layout_width="20dp"
        android:src="@drawable/ic_reduce"
        android:layout_height="20dp" />

    <TextView
        android:id="@+id/txt_sum"
        android:text="1"
        android:textSize="16sp"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/add"
        android:layout_width="20dp"
        android:src="@drawable/ic_add"
        android:layout_height="20dp" />

</LinearLayout>

2》

public class SumAddView extends LinearLayout {
    private Context context;

    private ImageView add;//增加产品数量按钮
    private ImageView reduce;//减少产品数量按钮
    private TextView sum;//展示产品数量

    private int totalSum = 1;//记录商品数量
    private int limitSum = 5;//最多添加几件商品

    //定义点击事件
    private SumClickListener listener;


    /**
     * 绑定点击事件
     *
     * @param listener
     */
    public void setSumClickListener(SumClickListener listener) {
        this.listener = listener;
    }

    public void setSum(int num) {
        totalSum = num;//设置初试数量
        this.sum.setText("" + num);
    }

    /**
     * 设置限购的数量
     *
     * @param limitSum
     */
    public void setLimitSum(int limitSum) {
        if (limitSum > 0) {
            this.limitSum = limitSum;
        } else {
            this.limitSum = 5;
        }
    }

    /**
     * 构造函数
     *
     * @param context
     * @param attrs
     */
    public SumAddView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        //初始化UI
        initView(context);

    }

    /**
     * 舒适化视图
     *
     * @param context
     */
    private void initView(Context context) {
        View view = View.inflate(context, R.layout.layout_add_sum_view, this);
        add = (ImageView) view.findViewById(R.id.add);
        reduce = (ImageView) view.findViewById(R.id.reduce);
        sum = (TextView) view.findViewById(R.id.txt_sum);

        //添加点击事件
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                addClick();
            }
        });

        //减少
        reduce.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                reduceClick();
            }
        });
    }

    /**
     * 增加商品数量
     */
    public void addClick() {
        totalSum++;
        if (totalSum >= limitSum) {
            totalSum = limitSum;//TODO
            Toast.makeText(context, "每人限购" + limitSum + "件", Toast.LENGTH_SHORT).show();
        }

        //回调事件
        if (listener != null) {
            listener.sumClick(totalSum);
        }

        sum.setText(totalSum + "");
    }

    /**
     * 减少商品数量
     */
    public void reduceClick() {
        totalSum--;
        if (totalSum > 0) {
            sum.setText(totalSum + "");
        } else {
            totalSum = 1;//TODO
            Toast.makeText(context, "不能再减少了", Toast.LENGTH_SHORT).show();
            sum.setText(totalSum + "");
        }

        //回调事件
        if (listener != null) {
            listener.sumClick(totalSum);
        }
    }

    /**
     * 回调商品数量点击事件
     */
    interface SumClickListener {
        void sumClick(int psum);
    }
}

6.activity

public class ShoppingCarActivity extends AppCompatActivity implements IView{
    CartAdapter cartAdapter;
    ExpandableListView listView;
    MyPresenter presenter;
    ImageView query_checked;
    TextView txt_total_price;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_car);

        presenter=new MyPresenter(this);
        presenter.getCarts();
        
        initView();
    }

    private void initView() {
        listView = (ExpandableListView) findViewById(R.id.expandable_listview);

        //TODO 设置二级列表,不能关闭
        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
        //TODO 去掉默认箭头
        listView.setGroupIndicator(null);

        cartAdapter = new CartAdapter(this);
        listView.setAdapter(cartAdapter);

        query_checked = (ImageView) findViewById(R.id.query_checked);
        txt_total_price=findViewById(R.id.txt_total_price);


        query_checked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        cartAdapter.setTotalPrice(txt_total_price);
        cartAdapter.setCheckAll(query_checked);
    }

    @Override
    public void showData(List<Shop> shops) {

    }

    @Override
    public void showCart(List<Seller> sellers) {
        cartAdapter.addData(sellers);//添加数据
        cartAdapter.expandGroup(listView);//展开列表
        cartAdapter.checkAll();//选中计算价格
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值