一级列表购物车完整版
来源:互联网 发布:埃德和沃伦夫妇知乎 编辑:程序博客网 时间:2024/04/23 21:09
----------------------------MainActivity------------------------------------------
package com.example.shopcarexam;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.widget.CheckBox;import android.widget.TextView;import java.util.ArrayList;import java.util.List;import de.greenrobot.event.EventBus;import de.greenrobot.event.Subscribe;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private CheckBox checkbox_all; private RecyclerView rv; private TextView tv_sum; private List<Bean> list = new ArrayList<>(); private MyAdapter myAdapter; private TextView tv_num; private int sumMoney = 0; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); initView(); rv.setLayoutManager(new LinearLayoutManager(this)); for (int i = 0; i < 10; i++) { Bean bean = new Bean(false, 1 + i, "购物车里的第" + i + "件商品"); list.add(bean); } myAdapter = new MyAdapter(this, list); rv.setAdapter(myAdapter); } private void initView() { rv = (RecyclerView) findViewById(R.id.rv); checkbox_all = (CheckBox) findViewById(R.id.checkbox_all); checkbox_all.setOnClickListener(this); tv_sum = (TextView) findViewById(R.id.tv_sum); tv_num = (TextView) findViewById(R.id.tv_num); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void allSelect(CheckEvent checkEvent){ checkbox_all.setChecked(checkEvent.isChecked()); } @Subscribe public void onMCEvent(MCEvent mcEvent){ if (mcEvent.isFlag()){ sumMoney=0; count=0; } int evNum = mcEvent.getEvNum(); int evPrice = mcEvent.getEvPrice(); sumMoney+=evPrice; count+=evNum; if (sumMoney<0||count<0){ sumMoney=0; count=0; } tv_num.setText("付款: "+count); tv_sum.setText(sumMoney+""); } /** * 总计 * @param */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.checkbox_all: myAdapter.selectAll(checkbox_all.isChecked()); break; } }}
-------------------MyAdapter---------------------------------------
package com.example.shopcarexam;import android.content.Context;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 android.widget.Toast;import java.util.List;import de.greenrobot.event.EventBus;/** * Created by hh on 2017/10/19. */public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<Bean> list; private int count = 0; public MyAdapter(Context context, List<Bean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final Bean bean = list.get(position); final MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.checkbox.setChecked(bean.isChecked()); myViewHolder.tv_title.setText(bean.getTitle()); myViewHolder.tv_price.setText(bean.getPrice() + ""); myViewHolder.add_del.setOnItemClick(new MyAddDelView.OnItemClick() { @Override public void onItemAddClick(int count) { if (bean.isChecked()) { MCEvent mcEvent = new MCEvent(); mcEvent.setEvNum(1); mcEvent.setEvPrice(bean.getPrice()); EventBus.getDefault().post(mcEvent); } else { Toast.makeText(context, "请勾选", Toast.LENGTH_SHORT).show(); myViewHolder.add_del.setCount(); } } @Override public void onItemDelClick(int count) { if (bean.isChecked()){ MCEvent mcEvent = new MCEvent(); mcEvent.setEvNum(-1); mcEvent.setEvPrice(-bean.getPrice()); EventBus.getDefault().post(mcEvent); }else{ Toast.makeText(context, "请勾选", Toast.LENGTH_SHORT).show(); myViewHolder.add_del.setCount(); } } }); /** * 复选框的选中监听事件 */ myViewHolder.checkbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //checkbox当选中的状态 if (myViewHolder.checkbox.isChecked()) { bean.setChecked(true); MCEvent mcEvent = new MCEvent(); mcEvent.setEvNum(1); mcEvent.setEvPrice(bean.getPrice()); EventBus.getDefault().post(mcEvent); //判断是否复选框全部勾选 if (isAllSelect()) { CheckEvent checkEvent = new CheckEvent(); checkEvent.setChecked(true); EventBus.getDefault().post(checkEvent); } } else { MCEvent mcEvent = new MCEvent(); mcEvent.setEvNum(-1); mcEvent.setEvPrice(-bean.getPrice()); EventBus.getDefault().post(mcEvent); bean.setChecked(false); //全选框取消 CheckEvent checkEvent = new CheckEvent(); checkEvent.setChecked(false); EventBus.getDefault().post(checkEvent); } } }); } @Override public int getItemCount() { return list.size(); } /** * 判断复选框是否全部选中 * * @return */ public boolean isAllSelect() { for (Bean bean : list) { if (!bean.isChecked()) { return false; } } return true; } /** * 判断全选 * * @param flag */ public void selectAll(boolean flag) { MCEvent mcEvent = new MCEvent(); mcEvent.setFlag(true); EventBus.getDefault().post(mcEvent); for (Bean bean : list) { if (flag) { MCEvent mcEvent1 = new MCEvent(); mcEvent1.setEvPrice(bean.getPrice()); mcEvent1.setEvNum(1); EventBus.getDefault().post(mcEvent1); } else { MCEvent mcEvent1 = new MCEvent(); mcEvent1.setEvPrice(-bean.getPrice()); mcEvent1.setEvNum(-1); EventBus.getDefault().post(mcEvent1); } bean.setChecked(flag); notifyDataSetChanged(); } } class MyViewHolder extends RecyclerView.ViewHolder { private final CheckBox checkbox; private final TextView tv_title; private final TextView tv_price; private final MyAddDelView add_del; public MyViewHolder(View itemView) { super(itemView); checkbox = itemView.findViewById(R.id.checkbox); tv_title = itemView.findViewById(R.id.tv_title); tv_price = itemView.findViewById(R.id.tv_price); add_del = itemView.findViewById(R.id.add_del); } }}
------------------MyAddDelView-----------------------------
package com.example.shopcarexam;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;/** * Created by hh on 2017/10/25. */public class MyAddDelView extends LinearLayout { private TextView num; private int count = 1; private OnItemClick onItemClick; public interface OnItemClick { public void onItemAddClick(int count); public void onItemDelClick(int count); } public void setOnItemClick(OnItemClick onItemClick) { this.onItemClick = onItemClick; } public MyAddDelView(Context context) { this(context, null); } public MyAddDelView(final Context context, @Nullable AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.add_jian_item, this); final TextView add = findViewById(R.id.add); TextView del = findViewById(R.id.del); num = findViewById(R.id.num); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { num.setText(++count + ""); onItemClick.onItemAddClick(1); } }); del.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (count != 1) { count = --count; onItemClick.onItemDelClick(-1); } num.setText(count >= 1 ? count + "" : 1 + ""); } }); } /** * 获取商品数量 * * @return */ public int getCount() { return count; } public void setCount() { count = 1; num.setText(count + ""); }}
--------------------------Bean--------------------------------------
package com.example.shopcarexam;/** * Created by hh on 2017/10/19. */public class Bean { private boolean checked; private int price; private String title; public Bean(boolean checked, int price, String title) { this.checked = checked; this.price = price; this.title = title; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }}
---------------------------------CheckEvent--------------------------------------------------
package com.example.shopcarexam;/** * Created by hh on 2017/10/22. */public class CheckEvent { private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; }}
-------------------------------MCEvent---------------------------------------------package com.example.shopcarexam;/** * Created by hh on 2017/10/22. */public class MCEvent { private int evPrice; private int evNum; private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public int getEvNum() { return evNum; } public void setEvNum(int evNum) { this.evNum = evNum; } public int getEvPrice() { return evPrice; } public void setEvPrice(int evPrice) { this.evPrice = evPrice; }}
--------------------MainActivity布局文件-----------------------------------------
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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" tools:context="com.example.shopcarexam.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="50dp" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:gravity="center" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="全选"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="合计:" /> <TextView android:id="@+id/tv_sum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="0" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="元" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/tv_num" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#ff3660" android:gravity="center" android:text="付款" /> </LinearLayout></RelativeLayout>
----------------------------------jia_jian_item布局---------------------------------------------<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="20dp" android:layout_height="20dp" android:id="@+id/del" android:gravity="center" android:background="@drawable/circle_shape" android:text="-"/> <TextView android:layout_width="40dp" android:layout_height="20dp" android:id="@+id/num" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/circle_shape" android:gravity="center" android:text="1"/> <TextView android:id="@+id/add" android:layout_width="20dp" android:layout_height="20dp" android:gravity="center" android:background="@drawable/circle_shape" android:text="+"/></LinearLayout>
-------------------------------------item布局------------------------------------------------
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="70dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:paddingLeft="10dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="10dp"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <com.example.shopcarexam.MyAddDelView android:layout_marginLeft="100dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_del"></com.example.shopcarexam.MyAddDelView> </LinearLayout></LinearLayout>
--------------------------------------drawable文件 circle_shape-----------------------------------------------
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="2dp" android:color="#33000000"></stroke> <corners android:radius="100dp"></corners></shape>
阅读全文
1 0
- 一级列表购物车完整版
- 一级列表购物车
- 一级列表购物车
- 购物车一级列表
- 一级列表的购物车
- 一级列表的购物车
- 仿购物车一级列表
- android一级列表购物车
- 购物车一级列表布局
- MVP一级列表购物车自定义View
- 二级列表购物车okhttp(完整版)
- 简单实现 一级列表购物车(固定数据)
- 购物车一级列表的简单实现(访问接口数据)
- 一级列表购物车的简单实现(网络接口)
- 购物车(map,一级列表,view绘制加减号)
- 一级购物车
- 一级购物车
- 一级购物车
- 手机口袋模式功能
- 如何能成为一个程序高手
- ThreadLocal(python 版)
- python multiprocessing 测试
- 线程高级编程
- 一级列表购物车完整版
- HLSL学习笔记
- 深度学习常用的数据集,包括各种数据跟图像数据。
- 2017年10月26日笔记
- 让机器人的学习更有趣的Fable
- Composer的安装和卸载
- 基于openVR实现任意软件界面映射到VR眼镜
- BZOJ 4934: [Ceoi2016]kangaroo
- Android Studio 3.0 最新稳定版下载