andriod——Retrofit+Fresco+MVP+Fragment里添加购物车

该博客展示了如何在Android应用中实现购物车功能,使用了Retrofit进行网络请求,Fresco处理图片显示,MVP模式进行架构设计,并在Fragment中进行展示。通过事件总线 EventBus 处理数据交互,包括商品选择和总价、数量的更新。
摘要由CSDN通过智能技术生成
package com.example.lenovo.fangjingdong.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.lenovo.fangjingdong.R;
import com.example.lenovo.fangjingdong.adapter.Rlv_Adapter;
import com.example.lenovo.fangjingdong.bean.CartBean;
import com.example.lenovo.fangjingdong.eventbusevent.MessageEvent;
import com.example.lenovo.fangjingdong.eventbusevent.PriceAndCountEvent;
import com.example.lenovo.fangjingdong.presenter.ShopCart_P;
import com.example.lenovo.fangjingdong.view.IShopCart_V;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;


public class FragmentFour extends Fragment implements View.OnClickListener, IShopCart_V {
    private CheckBox mFootBoxCart;
    private TextView mFootTotalPriceCart;
    private TextView mFootTotalCountCart;
    private List<CartBean.DataBean.ListBean> list;
    private RecyclerView mRlvMain;
    private Rlv_Adapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().unregister(this);
        View view=View.inflate(getActivity(), R.layout.activity_car,null);
        mFootBoxCart = (CheckBox)view.findViewById(R.id.cart_foot_box);
        mFootBoxCart.setOnClickListener(this);
        mFootTotalPriceCart = (TextView)view.findViewById(R.id.cart_foot_totalPrice);
        mFootTotalCountCart = (TextView)view.findViewById(R.id.cart_foot_totalCount);
        mRlvMain = (RecyclerView) view. findViewById(R.id.main_rlv);
        EventBus.getDefault().register(this);
        initView();

        return view;

    }

    private void initView() {
        list = new ArrayList<>();
        mRlvMain.setLayoutManager(new LinearLayoutManager(getActivity()));
        ShopCart_P shopCartP = new ShopCart_P(this);
        shopCartP.doGetCartBean();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cart_foot_box:
                // TODO 17/11/20
                break;
            default:
                break;
        }
    }

    @Override
    public void setCartBean(final CartBean cartBean) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //获取数据
                List<CartBean.DataBean> data = cartBean.getData();
                for (int i = 0; i < data.size(); i++) {
                    List<CartBean.DataBean.ListBean> listBeen = data.get(i).getList();
                    for (int j = 0; j < listBeen.size(); j++) {
                        list.add(listBeen.get(j));
                    }
                }
                adapter = new Rlv_Adapter(getContext(), list);
                mRlvMain.setAdapter(adapter);
            }
        });
    }

    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        mFootBoxCart.setChecked(event.isChecked());
    }
    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event) {
        mFootTotalPriceCart.setText("合计¥" + event.getPrice());
        mFootTotalCountCart.setText("结算(" + event.getCount() + ")");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值