商品详情界面点击跳转到购物车(二)购物车界面
来源:互联网 发布:影集制作软件免费版 编辑:程序博客网 时间:2024/05/16 19:29
//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.gaochao.shopcartdemo1.ShopCartActivity"> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="40dp"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选" /> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/cb" android:text="合计:" /> <TextView android:id="@+id/tvCount" android:layout_width="100dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:background="#ff0000" android:gravity="center" android:text="去结算(0)" android:textColor="#ffffff" /> </RelativeLayout></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/cbGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvGroup" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" /></LinearLayout>
<LinearLayout android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/cbChild" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv" android:layout_width="100dp" android:layout_height="100dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvSubhead" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/tvPrice" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_marginLeft="10dp" android:id="@+id/ivDel" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/iv_del" /> <TextView android:id="@+id/tvNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:text="1"/> <ImageView android:id="@+id/ivAdd" android:layout_width="25dp" android:layout_height="25dp" android:background="@drawable/iv_add" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/btDel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" /></LinearLayout>
//封装 两个bean 购物车商品界面bean 提取总价和结算总价 ShopCarBean priceAndCount
ShopCartBeanPriceAndCount
//m层
public interface ShopCartModelListener { void getCart(Map<String,String> params, OnNetListener<ShopCartBean> bean);}
public class ShopCartModel implements ShopCartModelListener { private Handler handler = new Handler(); @Override public void getCart(Map<String, String> params, final OnNetListener<ShopCartBean> onNetListener) { OkHttpUtils.getOkHttpUtils().doPost(API.str2, params, new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); final ShopCartBean shopCartBean = new Gson().fromJson(string, ShopCartBean.class); handler.post(new Runnable() { @Override public void run() { onNetListener.onSuccess(shopCartBean); } }); } }); }}//p层
public class ShopCartPresenter { private ShopCartViewListener shopCartViewListener; private ShopCartModelListener shopCartModelListener; public ShopCartPresenter(ShopCartViewListener shopCartViewListener) { this.shopCartViewListener = shopCartViewListener; shopCartModelListener = new ShopCartModel(); } public void onDettach(){ shopCartViewListener=null; } public void getShaopCartData(){ Map<String,String> params = new HashMap<>(); params.put("uid","1234"); params.put("pid","71"); shopCartModelListener.getCart(params, new OnNetListener<ShopCartBean>() { @Override public void onSuccess(ShopCartBean shopCartBean) { if (shopCartViewListener != null){ List<ShopCartBean.DataBean> group = shopCartBean.getData(); List<List<ShopCartBean.DataBean.ListBean>> child = new ArrayList<>(); for (int i=0;i<group.size();i++){ child.add(group.get(i).getList()); } shopCartViewListener.show(group,child); } } @Override public void onFailure(Exception e) { } }); }}
//v层
public interface ShopCartViewListener { void show(List<ShopCartBean.DataBean> group,List<List<ShopCartBean.DataBean.ListBean>> child);}
public class ShopCartActivity extends AppCompatActivity implements ShopCartViewListener { private ShopCartPresenter getCartPresenter; private ExpandableListView mElv; /** * 全选 */ private CheckBox mCb; /** * 合计: */ private TextView mTvTotal; /** * 去结算(0) */ private TextView mTvCount; private ElcAdapter elvAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shop_cart); initView(); getCartPresenter = new ShopCartPresenter(this); getCartPresenter.getShaopCartData(); mCb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { elvAdapter.AllOrNone(mCb.isChecked()); } }); } @Override protected void onDestroy() { super.onDestroy(); getCartPresenter.onDettach(); } @Override public void show(List<ShopCartBean.DataBean> group, List<List<ShopCartBean.DataBean.ListBean>> child) { elvAdapter = new ElcAdapter(this, group, child); mElv.setGroupIndicator(null); mElv.setAdapter(elvAdapter); for (int i = 0; i < group.size(); i++) { mElv.expandGroup(i); } } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCb = (CheckBox) findViewById(R.id.cb); mTvTotal = (TextView) findViewById(R.id.tvTotal); mTvCount = (TextView) findViewById(R.id.tvCount); } public void setPriceAndCount(PriceAndCount priceAndCount) { mTvTotal.setText("合计:" + priceAndCount.getPrice()); mTvCount.setText("去结算(" + priceAndCount.getCount() + ")"); } public void setAllChecked(boolean bool) { mCb.setChecked(bool); }}
//适配器
class ElcAdapter extends BaseExpandableListAdapter{ private Context context; private List<ShopCartBean.DataBean> group; private List<List<ShopCartBean.DataBean.ListBean>> child; private final LayoutInflater inflater; public ElcAdapter(Context context, List<ShopCartBean.DataBean> group, List<List<ShopCartBean.DataBean.ListBean>> child) { this.context = context; this.group = group; this.child = child; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return group.size(); } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return child.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view; final GroupViewHolder holder; if (convertView == null) { view = inflater.inflate(R.layout.group_item1, null); holder = new GroupViewHolder(); holder.tv = view.findViewById(R.id.tvGroup); holder.cbGroup = view.findViewById(R.id.cbGroup); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } final ShopCartBean.DataBean dataBean = group.get(groupPosition); holder.tv.setText(dataBean.getSellerName()); holder.cbGroup.setChecked(dataBean.isChecked()); holder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改变三个checkbox的状态值 //1.一级列表的checkbox状态值 dataBean.setChecked(holder.cbGroup.isChecked()); //2.二级列表的checkbox状态值 setChildrenCb(groupPosition, holder.cbGroup.isChecked()); //3.全选的checkbox状态值 ((ShopCartActivity) context).setAllChecked(isAllGroupCbChecked()); //计算钱和数量并显示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); return view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, final View convertView, ViewGroup parent) { View view; final ChildViewHolder holder; if (convertView == null) { view = inflater.inflate(R.layout.child_item2, null); holder = new ChildViewHolder(); holder.iv = view.findViewById(R.id.iv); holder.tvTitle = view.findViewById(R.id.tvTitle); holder.tvSubhead = view.findViewById(R.id.tvSubhead); holder.tvPrice = view.findViewById(R.id.tvPrice); holder.cbChild = view.findViewById(R.id.cbChild); holder.btDel = view.findViewById(R.id.btDel); holder.tvNum = view.findViewById(R.id.tvNum); holder.ivDel = view.findViewById(R.id.ivDel); holder.ivAdd = view.findViewById(R.id.ivAdd); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } final ShopCartBean.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition); String images = listBean.getImages(); Glide.with(context).load(images.split("\\|")[0]).into(holder.iv); holder.tvTitle.setText(listBean.getTitle()); holder.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked()); holder.tvSubhead.setText(listBean.getSubhead()); holder.tvPrice.setText(listBean.getPrice() + "元"); holder.tvNum.setText(listBean.getCount() + ""); //给checkbox设置点击事件 holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //需要改变三个checkbox的状态值 //1.二级列表的checkbox状态值 listBean.setChecked(holder.cbChild.isChecked()); //2.一级列表的checkbox状态值 group.get(groupPosition).setChecked(isAllChildCbChecked(groupPosition)); //3.全选的checkbox状态值 ((ShopCartActivity) context).setAllChecked(isAllGroupCbChecked()); //计算钱和数量并显示 setPriceAndCount(); //刷新界面 notifyDataSetChanged(); } }); holder.ivAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取目前显示的值 int count = listBean.getCount(); count++; //改变JavaBean里的状态值 listBean.setCount(count); //计算钱和数量并显示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); holder.ivDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取目前显示的值 int count = listBean.getCount(); if (count <= 1) { count = 1; } else { count--; } //改变JavaBean里的状态值 listBean.setCount(count); //计算钱和数量并显示 setPriceAndCount(); //刷新列表 notifyDataSetChanged(); } }); holder.btDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //其实就是删除集合 List<ShopCartBean.DataBean.ListBean> listBeans = child.get(groupPosition); if (listBeans.size() > 0) { listBeans.remove(childPosition); } if (listBeans.size() == 0) { child.remove(groupPosition); group.remove(groupPosition); } //计算钱和数量并显示 setPriceAndCount(); //改变全选状态 ((ShopCartActivity) context).setAllChecked(isAllGroupCbChecked()); //刷新列表 notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder { TextView tv; CheckBox cbGroup; } class ChildViewHolder { ImageView iv; TextView tvTitle; TextView tvSubhead; TextView tvPrice; CheckBox cbChild; Button btDel; TextView tvNum; ImageView ivDel; ImageView ivAdd; } /** * 设置一级列表对应的二级列表checkbox状态 * * @param groupPosition * @param bool */ private void setChildrenCb(int groupPosition, boolean bool) { List<ShopCartBean.DataBean.ListBean> listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { listBeans.get(i).setChecked(bool); } } /** * 判断一级列表checkbox状态 * * @return */ private boolean isAllGroupCbChecked() { if (group.size() == 0) { return false; } for (int i = 0; i < group.size(); i++) { if (!group.get(i).isChecked()) { return false; } } return true; } /** * 判断二级列表checkbox状态 * * @return */ private boolean isAllChildCbChecked(int groupPosition) { List<ShopCartBean.DataBean.ListBean> listBeans = child.get(groupPosition); for (int i = 0; i < listBeans.size(); i++) { if (!listBeans.get(i).isChecked()) { return false; } } return true; } /** * 设置钱和数量 */ private void setPriceAndCount() { ((ShopCartActivity) context).setPriceAndCount(compute()); } /** * 计算钱和数量 */ private PriceAndCount compute() { double price = 0; int count = 0; for (int i = 0; i < group.size(); i++) { List<ShopCartBean.DataBean.ListBean> listBeans = child.get(i); for (int j = 0; j < listBeans.size(); j++) { if (listBeans.get(j).isChecked()) { price += listBeans.get(j).getPrice() * listBeans.get(j).getCount(); count += listBeans.get(j).getCount(); } } } return new PriceAndCount(price, count); } /** * 全选或者全不选 * * @param bool */ public void AllOrNone(boolean bool) { for (int i = 0; i < group.size(); i++) { group.get(i).setChecked(bool); setChildrenCb(i, bool); } setPriceAndCount(); notifyDataSetChanged(); }}
阅读全文
0 0
- 商品详情界面点击跳转到购物车(二)购物车界面
- 商品详情页面点击跳转到购物车(一)详情界面
- 购物车详情界面点击加入购物车(四)
- 从详情页点击购物车按钮到主界面的购物车
- 仿京东详情页跳转到购物车
- 购物车之跳转到详情页面
- 商品详情,购物车,订单
- 详情页跳转购物车
- 购物车界面(Bootstrap)
- 商品详情页+添加购物车
- 动画动画商品详情加购物车
- 简易商品购物系统购物界面程序设计
- 商品详情页面,点击按钮将对应的商品加入购物车
- 仿京东商品详情页(配合购物车使用)
- Magento商品列表点击添加到购物车
- 购物车之详情页面跳转至购物车
- 动画加商品详情加商品购物车
- 跳转详情轮播图添加购物车
- 进程调度API之init_waitqueue_entry
- 亚马逊:内容付费重构了阅读新场景,阅读也早已跳出纸质书的概念
- Spring 框架简介
- poj2396 Budget(有源汇的有上下界的可行流)
- okhttp的二次封装
- 商品详情界面点击跳转到购物车(二)购物车界面
- SVN检出未响应问题
- 唯品会2017年双11大促技术保障实践,全域提供25万QPS服务能力
- Spring干货汇总(含Spring Boot与Spring Cloud)
- PTA计算平均工资
- ASIO 腾空出世 (那些年我们追过的网络库.PartII)
- BZOJ1552 [Cerc2007]robotic sort
- 腾讯帝国十八年,被它借鉴过的产品都有哪些?
- 2018 最具就业前景的 7 大编程语言,Java、Python 和 JavaScript 前三无悬念?