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);
}
}
andriod——Retrofit+Fresco+MVP+Fragment里添加购物车
最新推荐文章于 2021-05-26 11:12:16 发布
该博客展示了如何在Android应用中实现购物车功能,使用了Retrofit进行网络请求,Fresco处理图片显示,MVP模式进行架构设计,并在Fragment中进行展示。通过事件总线 EventBus 处理数据交互,包括商品选择和总价、数量的更新。
摘要由CSDN通过智能技术生成