Activity
布局import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; import com.bwie.Adapter.GouWuChe01; import com.bwie.bean.Shangpin; import com.bwie.bean.Shop; import com.jcodecraeer.xrecyclerview.XRecyclerView; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private XRecyclerView re_cy; private CheckBox te_check; private TextView te_jia; private Button te_jie; private List<Shop> list; private GouWuChe01 gouwuche01; private double money; private int sum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { list = new ArrayList<>(); for (int i = 0; i <10 ; i++) { List<Shangpin> splist = new ArrayList<>(); for (int j = 0; j <3 ; j++) { splist.add(new Shangpin(false,"商品"+(j+1),10.0+(j+1),1)); } list.add(new Shop(false,"商家"+(i+1),splist)); } setData(); } private void setData() { gouwuche01 = new GouWuChe01(MainActivity.this,list); re_cy.setLayoutManager(new LinearLayoutManager(MainActivity.this)); re_cy.setAdapter(gouwuche01); re_cy.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { Toast.makeText(MainActivity.this, "刷新数据", Toast.LENGTH_SHORT).show(); re_cy.refreshComplete(); } @Override public void onLoadMore() { Toast.makeText(MainActivity.this, "加载数据", Toast.LENGTH_SHORT).show(); re_cy.loadMoreComplete(); } }); gouwuche01.setZong(new GouWuChe01.Zong() { @Override public void shangjia() { boolean flag = true; for (Shop shop : list) { for (Shangpin shangpin : shop.getSplist()) { if (shangpin.isSpselect()==false) { flag = false; } } } te_check.setChecked(flag); } @Override public void shangpin() { boolean flag = true; for (Shop shop : list) { for (Shangpin shangpin : shop.getSplist()) { if(shangpin.isSpselect()==false) { flag=false; } } } te_check.setChecked(flag); } }); gouwuche01.setSjiage(new GouWuChe01.Sjjiage() { @Override public void sjjg() { money=0; for (Shop shop : list) { for (Shangpin shangpin : shop.getSplist()) { if(shangpin.isSpselect()==true) { money=money+(shangpin.getNum()*shangpin.getJiage()); } } } DecimalFormat decimalFormat = new DecimalFormat("######0.00"); te_jia.setText("¥"+decimalFormat.format(money)); } }); gouwuche01.setSjshuliang(new GouWuChe01.Sjshuliang() { @Override public void sjsl() { sum=0; for (Shop shop : list) { for (Shangpin shangpin : shop.getSplist()) { if(shangpin.isSpselect()==true) { sum++; } } } te_jie.setText("结算("+sum+")"); } }); } private void initView() { re_cy = (XRecyclerView) findViewById(R.id.re_cy); te_check = (CheckBox) findViewById(R.id.te_check); te_jia = (TextView) findViewById(R.id.te_jia); te_jie = (Button) findViewById(R.id.te_jie); te_check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { money=0; sum=0; for (Shop shop : list) { for (Shangpin shangpin : shop.getSplist()) { if(te_check.isChecked()) { shangpin.setSpselect(true); money = money+(shangpin.getNum()*shangpin.getJiage()); sum=(shop.getSplist().size())*10; }else{ shangpin.setSpselect(false); } } } gouwuche01.notifyDataSetChanged(); DecimalFormat decimalFormat = new DecimalFormat("######0.00"); te_jia.setText("¥"+decimalFormat.format(money)); te_jie.setText("结算("+sum+")"); } }); } }
外层<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.gouwuche.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="购物车" /> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/re_cy" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/relativeLayout" android:layout_below="@+id/textView"> </com.jcodecraeer.xrecyclerview.XRecyclerView> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:id="@+id/relativeLayout"> <CheckBox android:id="@+id/te_check" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="全选" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:gravity="center" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="合计:" android:layout_centerVertical="true" android:layout_toRightOf="@+id/te_check" android:layout_toEndOf="@+id/te_check" android:layout_marginLeft="14dp" android:layout_marginStart="14dp" /> <TextView android:id="@+id/te_jia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:text="TextView" /> <Button android:textColor="@android:color/white" android:background="@android:color/holo_red_dark" android:id="@+id/te_jie" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:text="结算" /> </RelativeLayout> </RelativeLayout>
布局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 com.bwie.Okhttp.Check; import com.bwie.bean.Shangpin; import com.bwie.bean.Shop; import com.example.gouwuche.R; import java.util.ArrayList; import java.util.List; /** * Created by GUODONG on 2017/10/25. */ public class GouWuChe01 extends RecyclerView.Adapter<GouWuChe01.ViewHloder> { private Context context; private List<Shop> list; public GouWuChe01(Context context, List<Shop> list) { this.context = context; this.list = list; } @Override public ViewHloder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); ViewHloder viewHloder = new ViewHloder(view); return viewHloder; } @Override public void onBindViewHolder(final ViewHloder holder, final int position) { holder.tv_shangjia.setText(list.get(position).getShangjia()); final List<Shangpin> splist = list.get(position).getSplist(); final GouWuChe02 gouWuChe02 = new GouWuChe02(context,splist); holder.re_cy02.setLayoutManager(new LinearLayoutManager(context)); holder.re_cy02.setAdapter(gouWuChe02); boolean flag = true; for (Shangpin shangpin : splist) { if(shangpin.isSpselect()==false) { flag = false; } } holder.te_check02.setChecked(flag); gouWuChe02.setGeshu(new GouWuChe02.Geshu() { @Override public void gs() { boolean flag = true; for (Shangpin shangpin : splist) { if(shangpin.isSpselect()==false) { flag=false; } } holder.te_check02.setChecked(flag); zong.shangpin(); } }); holder.te_check02.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for (Shangpin shangpin : splist) { if(holder.te_check02.isChecked()) { shangpin.setSpselect(true); }else{ shangpin.setSpselect(false); } } gouWuChe02.notifyDataSetChanged(); zong.shangjia(); sjjiage.sjjg(); sjshuliang.sjsl(); } }); gouWuChe02.setSpjiage(new GouWuChe02.Spjiage() { @Override public void spjg() { sjjiage.sjjg(); } }); gouWuChe02.setShulang(new GouWuChe02.Shuliang() { @Override public void spsl() { sjshuliang.sjsl(); } }); } @Override public int getItemCount() { return list.size(); } public static class ViewHloder extends RecyclerView.ViewHolder{ private CheckBox te_check02; private TextView tv_shangjia; private RecyclerView re_cy02; public ViewHloder(View itemView) { super(itemView); te_check02 = itemView.findViewById(R.id.te_check02); tv_shangjia = itemView.findViewById(R.id.tv_shangjia); re_cy02 = itemView.findViewById(R.id.re_cy02); } } private Zong zong; public void setZong(Zong zong) { this.zong = zong; } public interface Zong{ void shangjia(); void shangpin(); } private Sjjiage sjjiage; public void setSjiage(Sjjiage sjjiage) { this.sjjiage = sjjiage; } public interface Sjjiage{ void sjjg(); } private Sjshuliang sjshuliang; public void setSjshuliang(Sjshuliang sjshuliang) { this.sjshuliang = sjshuliang; } public interface Sjshuliang{ void sjsl(); } }
里层<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/te_check02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <TextView android:id="@+id/tv_shangjia" android:layout_marginLeft="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/re_cy02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/te_check02"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
布局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 com.bwie.Okhttp.Check; import com.bwie.bean.Shangpin; import com.bwie.bean.Shop; import com.example.gouwuche.R; import java.util.ArrayList; import java.util.List; /** * Created by GUODONG on 2017/10/25. */ public class GouWuChe01 extends RecyclerView.Adapter<GouWuChe01.ViewHloder> { private Context context; private List<Shop> list; public GouWuChe01(Context context, List<Shop> list) { this.context = context; this.list = list; } @Override public ViewHloder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.item, null); ViewHloder viewHloder = new ViewHloder(view); return viewHloder; } @Override public void onBindViewHolder(final ViewHloder holder, final int position) { holder.tv_shangjia.setText(list.get(position).getShangjia()); final List<Shangpin> splist = list.get(position).getSplist(); final GouWuChe02 gouWuChe02 = new GouWuChe02(context,splist); holder.re_cy02.setLayoutManager(new LinearLayoutManager(context)); holder.re_cy02.setAdapter(gouWuChe02); boolean flag = true; for (Shangpin shangpin : splist) { if(shangpin.isSpselect()==false) { flag = false; } } holder.te_check02.setChecked(flag); gouWuChe02.setGeshu(new GouWuChe02.Geshu() { @Override public void gs() { boolean flag = true; for (Shangpin shangpin : splist) { if(shangpin.isSpselect()==false) { flag=false; } } holder.te_check02.setChecked(flag); zong.shangpin(); } }); holder.te_check02.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for (Shangpin shangpin : splist) { if(holder.te_check02.isChecked()) { shangpin.setSpselect(true); }else{ shangpin.setSpselect(false); } } gouWuChe02.notifyDataSetChanged(); zong.shangjia(); sjjiage.sjjg(); sjshuliang.sjsl(); } }); gouWuChe02.setSpjiage(new GouWuChe02.Spjiage() { @Override public void spjg() { sjjiage.sjjg(); } }); gouWuChe02.setShulang(new GouWuChe02.Shuliang() { @Override public void spsl() { sjshuliang.sjsl(); } }); } @Override public int getItemCount() { return list.size(); } public static class ViewHloder extends RecyclerView.ViewHolder{ private CheckBox te_check02; private TextView tv_shangjia; private RecyclerView re_cy02; public ViewHloder(View itemView) { super(itemView); te_check02 = itemView.findViewById(R.id.te_check02); tv_shangjia = itemView.findViewById(R.id.tv_shangjia); re_cy02 = itemView.findViewById(R.id.re_cy02); } } private Zong zong; public void setZong(Zong zong) { this.zong = zong; } public interface Zong{ void shangjia(); void shangpin(); } private Sjjiage sjjiage; public void setSjiage(Sjjiage sjjiage) { this.sjjiage = sjjiage; } public interface Sjjiage{ void sjjg(); } private Sjshuliang sjshuliang; public void setSjshuliang(Sjshuliang sjshuliang) { this.sjshuliang = sjshuliang; } public interface Sjshuliang{ void sjsl(); } }
bean类1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/te_check02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <TextView android:id="@+id/tv_shangjia" android:layout_marginLeft="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/re_cy02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/te_check02"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
bean类2public class Shangpin { private boolean spselect; private String sp; private double jiage; private int num; @Override public String toString() { return "Shangpin{" + "spselect=" + spselect + ", sp='" + sp + '\'' + ", jiage=" + jiage + ", num=" + num + '}'; } public void setSpselect(boolean spselect) { this.spselect = spselect; } public void setSp(String sp) { this.sp = sp; } public void setJiage(double jiage) { this.jiage = jiage; } public void setNum(int num) { this.num = num; } public boolean isSpselect() { return spselect; } public String getSp() { return sp; } public double getJiage() { return jiage; } public int getNum() { return num; } public Shangpin() { } public Shangpin(boolean spselect, String sp, double jiage, int num) { this.spselect = spselect; this.sp = sp; this.jiage = jiage; this.num = num; } }
加减器public class Shop { private boolean select; private String shangjia; private List<Shangpin> splist; @Override public String toString() { return "Shop{" + "select=" + select + ", shangjia='" + shangjia + '\'' + ", splist=" + splist + '}'; } public void setSelect(boolean select) { this.select = select; } public void setShangjia(String shangjia) { this.shangjia = shangjia; } public void setSplist(List<Shangpin> splist) { this.splist = splist; } public Shop() { } public boolean isSelect() { return select; } public String getShangjia() { return shangjia; } public List<Shangpin> getSplist() { return splist; } public Shop(boolean select, String shangjia, List<Shangpin> splist) { this.select = select; this.shangjia = shangjia; this.splist = splist; } }
public class AmountView extends LinearLayout implements View.OnClickListener, TextWatcher { private static final String TAG = "AmountView"; private int amount = 1; //购买数量 private int goods_storage = 1; //商品库存 private OnAmountChangeListener mListener; private EditText etAmount; private Button btnDecrease; private Button btnIncrease; public AmountView(Context context) { this(context, null); } public AmountView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.view_amount, this); etAmount = (EditText) findViewById(R.id.etAmount); btnDecrease = (Button) findViewById(R.id.btnDecrease); btnIncrease = (Button) findViewById(R.id.btnIncrease); btnDecrease.setOnClickListener(this); btnIncrease.setOnClickListener(this); etAmount.addTextChangedListener(this); TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView); int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnWidth, LayoutParams.WRAP_CONTENT); int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 80); int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0); int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 0); obtainStyledAttributes.recycle(); LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT); btnDecrease.setLayoutParams(btnParams); btnIncrease.setLayoutParams(btnParams); if (btnTextSize != 0) { btnDecrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize); btnIncrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize); } LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT); etAmount.setLayoutParams(textParams); if (tvTextSize != 0) { etAmount.setTextSize(tvTextSize); } } public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) { this.mListener = onAmountChangeListener; } public void setGoods_storage(int goods_storage) { this.goods_storage = goods_storage; } @Override public void onClick(View v) { int i = v.getId(); if (i == R.id.btnDecrease) { if (amount > 1) { amount--; etAmount.setText(amount + ""); } } else if (i == R.id.btnIncrease) { if (amount < goods_storage) { amount++; etAmount.setText(amount + ""); } } etAmount.clearFocus(); if (mListener != null) { mListener.onAmountChange(this, amount); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { if (s.toString().isEmpty()) return; amount = Integer.valueOf(s.toString()); if (amount > goods_storage) { etAmount.setText(goods_storage + ""); return; } if (mListener != null) { mListener.onAmountChange(this, amount); } } public interface OnAmountChangeListener { void onAmountChange(View view, int amount); } }