shopcar

来源:互联网 发布:spss数据分析满意度 编辑:程序博客网 时间:2024/06/01 18:35

因为我们是本地购物车

所以先建立本地的bean类

public class ShopLiBean {  public String sellerName;    public List<DataBean> list;    public ShopLiBean(String sellerName,List<DataBean> list){        this.sellerName=sellerName;        this.list=list;    } public static class DataBean{       public String producrName;        public double price;        public boolean isSelect;        public int text_sum;        public DataBean(String producrName,double price,boolean isSelect,int text_sum){            this.producrName=producrName;            this.price=price;            this.isSelect=isSelect;            this.text_sum=text_sum;        } }}
Main中代码

package test.bwie.myapplication;import android.content.Context;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.text.DecimalFormat;import java.util.ArrayList;import java.util.List;import test.bwie.myapplication.bean.ShopLiBean;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private RecyclerView recycle;    private List<ShopLiBean> list;    private CheckBox tv_quanxuan;    private ShopAdapter shopAdapter;    private double sumPrice;    private int sumNum;    private TextView tv_sum;    private TextView sum;    private DecimalFormat decimalFormat;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        inteDate();        decimalFormat = new DecimalFormat("#0.00");        recycle.setLayoutManager(new LinearLayoutManager(this));        shopAdapter = new ShopAdapter(this,list);        recycle.setAdapter(shopAdapter);        shopAdapter.setOnItemChickListener(new ShopAdapter.OnItemChickListener() {           @Override           public void shopinter(int a, int b) {               if (list.get(a).list.get(b).isSelect){                   sumPrice=sumPrice+(list.get(a).list.get(b).price)*(list.get(a).list.get(b).text_sum);                   sumNum=sumNum+list.get(a).list.get(b).text_sum;               }else {                   sumPrice=sumPrice-(list.get(a).list.get(b).price)*(list.get(a).list.get(b).text_sum);                   sumNum= sumNum-list.get(a).list.get(b).text_sum;               }               tv_sum.setText(decimalFormat.format(sumPrice));               sum.setText("去结算(" + sumNum + ")");               isCheckAllCheck();           }        });    }    private void inteDate() {        list=new ArrayList<>();        for (int i = 0; i <5 ; i++) {             List<ShopLiBean.DataBean> dataBean=new ArrayList<>();            for (int j = 0; j <3 ; j++) {                dataBean.add(new ShopLiBean.DataBean("商品"+j,10*(j+1),false,1));            }            list.add(new ShopLiBean("商家"+i,dataBean));        }    }  private void  isCheckAllCheck(){      boolean flag=true;      for (ShopLiBean shopLiBean : list) {          for (ShopLiBean.DataBean dataBean : shopLiBean.list) {              if (!dataBean.isSelect){                  flag=false;                  break;              }          }      }    tv_quanxuan.setChecked(flag);  }    private void initView() {        recycle = (RecyclerView) findViewById(R.id.shopcar_recycle);        tv_quanxuan = (CheckBox) findViewById(R.id.checkbox_quanxuan);        tv_quanxuan.setOnClickListener(this);        tv_sum = (TextView) findViewById(R.id.tv_sum);        sum = (TextView) findViewById(R.id.jiesuan_button);    }    @Override    public void onClick(View view) {        selectall();    }    private void selectall() {        for (ShopLiBean shopLiBean : list) {            for (ShopLiBean.DataBean dataBean : shopLiBean.list) {                if (tv_quanxuan.isChecked()){                    if (!dataBean.isSelect) {                        dataBean.isSelect = true;                        sumPrice=sumPrice+dataBean.price*dataBean.text_sum;                        sumNum=sumNum+dataBean.text_sum;                    }                    }else {                        if (dataBean.isSelect){                            dataBean.isSelect=false;                            sumPrice=sumPrice-dataBean.price*dataBean.text_sum;                            sumNum=sumNum-dataBean.text_sum;                        }                    }                }            }        shopAdapter.notifyDataSetChanged();        tv_sum.setText(decimalFormat.format(sumPrice));        sum.setText("去结算(" + sumNum + ")");    }}
第一层adapter

package test.bwie.myapplication;import android.content.Context;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.CheckBox;import android.widget.TextView;import java.util.List;import test.bwie.myapplication.bean.ShopLiBean;/** * Created by 蒋六六 on 2017/10/25. */public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopHolder> {    private Context content;    private List<ShopLiBean> list;    private OnItemChickListener onItemChickListener;    public void setOnItemChickListener(OnItemChickListener onItemChickListener) {        this.onItemChickListener = onItemChickListener;    }    public ShopAdapter(Context context, List<ShopLiBean> list) {        this.content=context;        this.list=list;    }    @Override    public ShopHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View in = View.inflate(content, R.layout.shapcar1_item, null);        ShopHolder shop=new ShopHolder(in);        return shop;    }    @Override    public void onBindViewHolder(final ShopHolder holder, final int position) {        final ShopLiBean shopLiBean = list.get(position);         ischeckshop(holder,shopLiBean);        holder.tv_shophome.setText(shopLiBean.sellerName);        holder.shop_recycle.setLayoutManager(new LinearLayoutManager(content));      final   ShopliAdapter shopliadapter=new ShopliAdapter(content,shopLiBean.list);        holder.shop_recycle.setAdapter(shopliadapter);       if (onItemChickListener!=null){           shopliadapter.setOnItemCheck(new ShopliAdapter.OnItemCheckListener() {               @Override               public void ok(int pos) {                   onItemChickListener.shopinter(holder.getLayoutPosition(),pos);                   ischeckshop(holder,shopLiBean);               }           });           holder.ck_shophome.setOnClickListener(new View.OnClickListener() {               @Override               public void onClick(View view) {                   for (ShopLiBean.DataBean dataBean : shopLiBean.list) {                       if (holder.ck_shophome.isChecked()){                           dataBean.isSelect=true;                       }else {                           dataBean.isSelect=false;                       }                   }                   shopliadapter.notifyDataSetChanged();               }           });       }    }    private void ischeckshop(ShopHolder holder, ShopLiBean shopLiBean) {        boolean f=true;        for (ShopLiBean.DataBean dataBean : shopLiBean.list) {            if (!dataBean.isSelect){                f=false;               break;            }        }     holder.ck_shophome.setChecked(f);    }    @Override    public int getItemCount() {        return list.size();    }    class ShopHolder extends RecyclerView.ViewHolder{        private CheckBox ck_shophome;        private TextView tv_shophome;        private RecyclerView shop_recycle;        public ShopHolder(View itemView) {            super(itemView);            ck_shophome = itemView.findViewById(R.id.ck_shophome);            shop_recycle= itemView.findViewById(R.id.shop_li_recy);            tv_shophome = itemView.findViewById(R.id.ck_shophome);        }    }    public interface OnItemChickListener {        void shopinter(int a, int b);    }}
第二层adapter

package test.bwie.myapplication;import android.content.Context;import android.support.v7.widget.RecyclerView;import android.view.View;import android.view.ViewGroup;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.TextView;import java.text.DecimalFormat;import java.util.List;import test.bwie.myapplication.bean.ShopLiBean;import test.bwie.myapplication.view.AddSubView;public class ShopliAdapter extends RecyclerView.Adapter<ShopliAdapter.AAAAA> {    private List<ShopLiBean.DataBean> list;    private Context context;    public OnItemCheckListener onItemCheckListener;    public void setOnItemCheck(OnItemCheckListener onItemCheckListener){        this.onItemCheckListener=onItemCheckListener;    }    public  ShopliAdapter(Context content, List<ShopLiBean.DataBean> list) {        this.list=list;        this.context=content;    }    @Override    public AAAAA onCreateViewHolder(ViewGroup parent, int viewType) {        View inflate = View.inflate(context, R.layout.shopcar2_item, null);        AAAAA adap=new AAAAA(inflate);        return adap;    }    @Override    public void onBindViewHolder(final AAAAA holder, final int position) {        final ShopLiBean.DataBean dataBean = list.get(position);        if (dataBean.isSelect){            holder.ck.setChecked(true);        }else {            holder.ck.setChecked(false);        }        holder.num.setNum(list.get(position).text_sum+"");        holder.tv_title.setText(dataBean.producrName);        DecimalFormat decima=new DecimalFormat("#0.00");        holder.tv.setText(decima.format(dataBean.price));        if(onItemCheckListener!=null){            holder.ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                @Override                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {                     dataBean.isSelect=b;                    onItemCheckListener.ok(holder.getLayoutPosition());                }            });        }        //自定义控件的赋值        holder.num.setAddSubClickListener(new AddSubView.AddSubClickListener() {            @Override            public void addClickListener(int n) {                  list.get(position).text_sum=n;            }        });    }    @Override    public int getItemCount() {        return list.size();    }    class AAAAA extends RecyclerView.ViewHolder{        private CheckBox ck;        private TextView tv;        private TextView tv_title;        private AddSubView num;        public AAAAA(View itemView) {            super(itemView);            tv_title = itemView.findViewById(R.id.tv_jianjie);            ck = itemView.findViewById(R.id.ck_gt);            tv = itemView.findViewById(R.id.tv_shop_price);            num = itemView.findViewById(R.id.nnn);        }    }    public interface OnItemCheckListener{        void ok(int pos);    }}
//灰色背景边框
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"    >    <solid android:color="#fff"></solid><stroke android:color="#666" android:width="1px"></stroke>    <size android:width="10dp" android:height="10dp"></size></shape>
//自定义加减号
package test.bwie.myapplication.view;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;import test.bwie.myapplication.R;public class AddSubView extends LinearLayout implements View.OnClickListener {    private Context context;    private TextView mTvSub;    private TextView mTvNum;    private TextView mTvAdd;    private AddSubClickListener addSubClickListener;    public AddSubView(Context context) {        this(context, null);    }    public AddSubView(Context context, @Nullable AttributeSet attrs) {        this(context, attrs, 0);    }    public AddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        this.context = context;        initView();    }    private void initView() {        View view = View.inflate(context, R.layout.add_sub_view_layout, this);        mTvSub = (TextView) view.findViewById(R.id.tv_sub);        mTvNum = (TextView) view.findViewById(R.id.tv_num);        mTvAdd = (TextView) view.findViewById(R.id.tv_add);        mTvSub.setOnClickListener(this);        mTvAdd.setOnClickListener(this);    }    public void setAddSubClickListener(AddSubClickListener addSubClickListener) {        this.addSubClickListener = addSubClickListener;    }    public void setNum(String num) {        int i = Integer.parseInt(num);        if (i > 9) {            mTvAdd.setEnabled(false);        } else if (i < 2) {            mTvSub.setEnabled(false);        }        mTvNum.setText(num);    }    public String getNum() {        return mTvNum.getText().toString();    }    @Override    public void onClick(View view) {        if (addSubClickListener != null){            int num = Integer.parseInt(mTvNum.getText().toString());            switch (view.getId()){                case R.id.tv_add:                    num++;                    if (!mTvSub.isEnabled()) {                        mTvSub.setEnabled(true);                    }                    if (num > 9) {                        mTvAdd.setEnabled(false);                    }                    break;                case R.id.tv_sub:                    num--;                    if (!mTvAdd.isEnabled()) {                        mTvAdd.setEnabled(true);                    }                    if (num < 2) {                        mTvSub.setEnabled(false);                    }                    break;            }            addSubClickListener.addClickListener(num);            mTvNum.setText(Integer.toString(num));        }    }    public interface AddSubClickListener {        void addClickListener(int num);    }}
//xmml
<?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:gravity="center_vertical"    android:orientation="horizontal">    <TextView        android:id="@+id/tv_sub"        android:layout_width="20dp"        android:layout_height="20dp"        android:gravity="center"        android:background="@drawable/add_sub_view_bg"        android:text="-" />    <TextView        android:id="@+id/tv_num"        android:layout_width="wrap_content"        android:layout_height="20dp"        android:layout_weight="1"        android:inputType="number"        android:gravity="center"        android:background="@drawable/add_sub_view_bg"        android:text="1" />    <TextView        android:background="@drawable/add_sub_view_bg"        android:id="@+id/tv_add"        android:layout_width="20dp"        android:gravity="center"        android:layout_height="20dp"        android:text="+" />    </LinearLayout>



原创粉丝点击