想要实现购物车功能首先导入一个RecycleView的依赖
MainActivity类
public class MainActivity extends AppCompatActivity { private CheckBox checkBox; private TextView all_price; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox = (CheckBox) findViewById(R.id.ck_all); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_car); all_price = (TextView) findViewById(R.id.all_price); LinearLayoutManager manager = new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); //添加死数据 final List<CarBean> list = new ArrayList<>(); for (int i = 0; i < 5; i++) { list.add(new CarBean(R.mipmap.ic_launcher_round, "商品" + i, 100, true, 1)); } final CarAdapter adapter = new CarAdapter(this, list); recyclerView.setAdapter(adapter); adapter.setCheckListener(new CarAdapter.CheckListener() { @Override public void check(boolean check, int position) { //监听全选 boolean all_check = true; //价格 float price = 0; int count = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).isB()) { price += list.get(i).getPrice() * list.get(i).getCount(); count += list.get(i).getCount(); } } all_price.setText("共计" + count + "件,总共" + price + "元"); for (int i = 0; i < list.size(); i++) { if (!list.get(i).isB()) { all_check = false; break; } } if (all_check) { checkBox.setChecked(true); } else { checkBox.setChecked(false); } } }); //全选和反选 checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean check = checkBox.isChecked(); float price = 0; int count = 0; for (int i = 0; i < list.size(); i++) { list.get(i).setB(check); if (list.get(i).isB()) { price += list.get(i).getPrice() * list.get(i).getCount(); count += list.get(i).getCount(); } } adapter.notifyDataSetChanged(); all_price.setText("共计" + count + "件,总共" + price + "元"); } }); } }Main布局
<?xml version="1.0" encoding="utf-8"?> <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.gouwuche.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="购物车" android:textSize="28sp" /> <android.support.v7.widget.RecyclerView android:id="@+id/rv_car" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="30dp" android:orientation="horizontal"> <CheckBox android:id="@+id/ck_all" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:checked="true" android:text="全选" /> <TextView android:id="@+id/all_price" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_weight="1" android:gravity="center_vertical" android:text="总计5件,总价500元" android:textSize="10sp" /> </LinearLayout> </LinearLayout>Adapter适配器
public class CarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<CarBean> list; public CarAdapter(Context context, List<CarBean> list) { this.context = context; this.list = list; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.car_item,parent,false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { if(holder instanceof ViewHolder){ ViewHolder viewHolder = (ViewHolder)holder; if(list.get(position).isB()){ viewHolder.item_ck.setChecked(true); }else{ viewHolder.item_ck.setChecked(false); } viewHolder.item_name.setText(list.get(position).getName()); viewHolder.item_price.setText("单价:"+list.get(position).getPrice()+" ¥"); viewHolder.item_des.setText("共计"+list.get(position).getCount()+"件,总价"+list.get(position).getCount()*100+"元"); viewHolder.item_ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { boolean isChecked = list.get(position).isB(); list.get(position).setB(!isChecked); notifyDataSetChanged(); if(listener != null){ listener.check(!isChecked,position); } } }); //监听Edtext viewHolder.item_custom.setListener(new CustomView.ChangeListener() { @Override public void onChange(long count) { list.get(position).setCount(count); notifyDataSetChanged(); if(listener != null){ listener.check(list.get(position).isB(),position); } } }); viewHolder.del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (list.get(position).isB()){ Toast.makeText(context, "不能删除", Toast.LENGTH_SHORT).show(); }else { list.remove(position); notifyDataSetChanged(); if(listener != null){ listener.check(true,position); } } } }); } } @Override public int getItemCount() { return list.size(); } //为check写一个接口 public CheckListener listener; public void setCheckListener(CheckListener listener){ this.listener = listener; } public interface CheckListener{ public void check(boolean check, int position); } class ViewHolder extends RecyclerView.ViewHolder{ private final CustomView item_custom; private final TextView item_name; private final TextView item_price; private final TextView item_des; private final CheckBox item_ck; private final Button del; public ViewHolder(View itemView) { super(itemView); item_custom = (CustomView) itemView.findViewById(R.id.item_custom); item_name = (TextView) itemView.findViewById(R.id.item_name); item_ck = (CheckBox) itemView.findViewById(R.id.item_ck); item_price = itemView.findViewById(R.id.item_price); item_des = itemView.findViewById(R.id.item_des); del = itemView.findViewById(R.id.delete); } } }item布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="130dp"> <CheckBox android:id="@+id/item_ck" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/car_img" android:layout_toRightOf="@id/item_ck" android:src="@mipmap/ic_launcher" android:layout_margin="5dp" android:layout_width="60dp" android:layout_centerVertical="true" android:layout_height="60dp" /> <TextView android:id="@+id/item_name" android:layout_toRightOf="@id/car_img" android:layout_width="wrap_content" android:text="111" android:layout_marginTop="30dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/item_price" android:layout_toRightOf="@id/car_img" android:layout_width="wrap_content" android:text="111" android:layout_below="@id/item_name" android:layout_height="wrap_content" /> <com.example.gouwuche.CustomView android:id="@+id/item_custom" android:layout_below="@id/item_price" android:layout_toRightOf="@id/car_img" android:layout_width="70dp" android:layout_height="70dp"/> <Button android:id="@+id/delete" android:layout_alignParentRight="true" android:layout_margin="5dp" android:text="删除" android:layout_centerVertical="true" android:layout_width="40dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/item_des" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/car_img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>javaBean
public class CarBean { private int pic; private String name; private int price; private boolean b=true; private long count; public CarBean(int pic, String name, int price, boolean isSelect, long count) { this.pic = pic; this.name = name; this.price = price; this.b = isSelect; this.count = count; } public int getPic() { return pic; } public void setPic(int pic) { this.pic = pic; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public boolean isB() { return b; } public void setB(boolean b) { this.b = b; } public long getCount() { return count; } public void setCount(long count) { this.count = count; } }自定义layout布局
public class CustomView extends LinearLayout{ public CustomView(Context context) { super(context); } public CustomView(final Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.custom_layout,null); addView(view); Button jian = (Button) view.findViewById(R.id.jian); Button jia = (Button) view.findViewById(R.id.jia); final EditText num = (EditText) view.findViewById(R.id.num); //减号 jian.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String result = num.getText().toString().trim(); int integerResult = Integer.valueOf(result); //给做个限制 if(integerResult > 1){ integerResult = integerResult - 1; num.setText(integerResult + ""); }else{ Toast.makeText(context, "最小数量为1", Toast.LENGTH_SHORT).show(); } if(listener != null){ listener.onChange(integerResult); } } }); //加号 jia.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String result = num.getText().toString().trim(); int integerResult = Integer.valueOf(result); if(integerResult < 10000){ integerResult = integerResult + 1; num.setText(integerResult +""); }else{ Toast.makeText(context, "已经超出最大值", Toast.LENGTH_SHORT).show(); } if(listener != null ){ listener.onChange(integerResult); } } }); num.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.i("beforeTextChanged",s+"-----"+start+"-------"+count+"-----"+after); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.i("onTextChanged",s+"-----"+start+"-------"+before+"-----"+count); } @Override public void afterTextChanged(Editable s) { if(listener != null ){ listener.onChange(Long.valueOf(s.toString())); } } }); } public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ChangeListener listener; public void setListener(ChangeListener listener){ this.listener = listener; } public interface ChangeListener{ public void onChange(long count); } }布局效果
<?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"> <Button android:layout_width="30dp" android:layout_height="30dp" android:text="-" android:background="#fff" android:id="@+id/jian" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:text="1" android:id="@+id/num"/> <Button android:layout_width="30dp" android:layout_height="30dp" android:background="#fff" android:text="+" android:id="@+id/jia"/> </LinearLayout>