Android 二级列表仿京东购物车 《H》

效果图:
   
所需图片:
分别是:shop_icon;shopcart_add_red;shopcart_minus_grey;shopcart_minus_red

     在敲代码之前需在Bean中手动添加几个属性:  num:计算总价 ;
                                                                                    cou:计算选中商品数量默认是 1
                                                                        oneischeck:一级列表是否被选中
                                                                        twoischeck:二级列表是否被选中
      
MainActivity页面
public class ThreeFrgment extends Fragment implements Carview {

    private View view;
    private ExpandableListView car_edl;
    private ImageView shopping_cart_title_img;
    private TextView shopping_cart_title_edit;
    private TextView shopping_cart_bottom_Price;
    private TextView shopping_cart_bottom_jiesuan;
    private CheckBox shopping_cart_bottom_CheckBox;
    private List<CarBean.DataBean> list = new ArrayList<CarBean.DataBean>();
    private ThreeAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.threefragment, container, false);
        //注册EventBus
        EventBus.getDefault().register(this);
        initview();
        huoqu();
        shopping_cart_bottom_CheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //判断全选的状态
                adapter.checkALLStatus(shopping_cart_bottom_CheckBox.isChecked());

            }
        });

        adapter = new ThreeAdapter(getActivity(), list);
        car_edl.setAdapter(adapter);

        //将一级列表的展开标志隐藏
        car_edl.setGroupIndicator(null);

        adapter.notifyDataSetChanged();
        return view;

    }

    private void huoqu() {
        ThreePresaenter presaenter = new ThreePresaenter();
        presaenter.attchview(this);
        presaenter.getnews();
    }

    private void initview() {
        shopping_cart_title_img = (ImageView) view.findViewById(R.id.shopping_cart_title_img);//返回按钮
        shopping_cart_title_edit = (TextView) view.findViewById(R.id.shopping_cart_title_edit);//编辑
        shopping_cart_bottom_Price = (TextView) view.findViewById(R.id.shopping_cart_bottom_Price);//总价格
        shopping_cart_bottom_jiesuan = (TextView) view.findViewById(R.id.shopping_cart_bottom_jiesuan);//结算
        shopping_cart_bottom_CheckBox = (CheckBox) view.findViewById(R.id.shopping_cart_bottom_CheckBox);//全选
        car_edl = (ExpandableListView) view.findViewById(R.id.car_edl);
    }


    @Override
    public void success(List<CarBean.DataBean> news) {
        if (news != null) {
            list.addAll(news);
            //循环集合
            for (int i = 0; i < list.size(); i++) {
                //将二级列表展开
                car_edl.expandGroup(i);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void failed(Exception e) {
        Log.d("Car错误:", e.getMessage());
    }

    //接收适配器传过来的值
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onMessageEvent(boolean event) {
        //将全选的状态设置为true或者false
        shopping_cart_bottom_CheckBox.setChecked(event);
    }

    //接收适配器传过来的数量和总价
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onMessageEvent(CountPrice event) {
        shopping_cart_bottom_jiesuan.setText("结算(" + event.getCount() + ")" + "");
        shopping_cart_bottom_Price.setText("总价:¥"+event.getPrice() + "");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

注:CountPrice是自定义的一个类存放结算和价格的变量,为了让EventBus发送一个对象;
MainActivity布局页面
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用jQuery仿京东购物车的示例代码: ```html <!DOCTYPE html> <html> <head> <title>jQuery仿京东购物车</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { // 添加商品到购物车 $(".add-to-cart").click(function() { var product = $(this).closest(".product"); var productName = product.find(".product-name").text(); var productPrice = product.find(".product-price").text(); var cartItem = $("<div class='cart-item'></div>"); cartItem.append("<span class='item-name'>" + productName + "</span>"); cartItem.append("<span class='item-price'>" + productPrice + "</span>"); $(".cart-items").append(cartItem); }); // 从购物车中移除商品 $(document).on("click", ".remove-from-cart", function() { $(this).closest(".cart-item").remove(); }); }); </script> <style> .product { margin-bottom: 10px; } .cart-item { margin-bottom: 5px; } </style> </head> <body> <h1>商品列表</h1> <div class="product"> <span class="product-name">商品1</span> <span class="product-price">100元</span> <button class="add-to-cart">加入购物车</button> </div> <div class="product"> <span class="product-name">商品2</span> <span class="product-price">200元</span> <button class="add-to-cart">加入购物车</button> </div> <h1>购物车</h1> <div class="cart-items"></div> </body> </html> ``` 这段代码实现了一个简单的购物车功能。当点击"加入购物车"按钮时,会将商品名称和价格添加到购物车中。购物车中的商品可以通过点击"移除"按钮来移除。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值