<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.goodcart1.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#44eee2">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:textSize="20sp"/>
<TextView
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="结算"
android:textSize="20sp"
android:background="#99ee22"
android:gravity="center"
android:layout_marginLeft="200dp"/>
</LinearLayout>
</LinearLayout>
<?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="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/jian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="30dp"/>
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:paddingLeft="5dp"
android:textSize="20dp"/>
<TextView
android:id="@+id/jia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:paddingLeft="5dp"
android:textSize="25dp"/>
</LinearLayout>
</LinearLayout>
/**
* Created by user on 2017/11/17.
* 购物车接口
*/
public interface IShowCart {
public void check(int num);
public void allCheak(int num);
public void getPrice();
}
/**
* Created by user on 2017/11/17.
* 展示数据的类
*/
public class GoodBean {
boolean check;
String name;
int price;
int num;
public GoodBean(int num, boolean check, String name, int price) {
this.num = num;
this.check = check;
this.name = name;
this.price = price;
}
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
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 int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
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.TextView;
import java.util.List;
/**
* Created by user on 2017/11/17.
* 适配器
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<GoodBean> list;
Context context;
IShowCart iShowCart;
int num;
public MyAdapter(List<GoodBean> list, Context context, IShowCart iShowCart) {
this.list = list;
this.context = context;
this.iShowCart = iShowCart;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.item, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final GoodBean goodBean = list.get(position);
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.check.setChecked(goodBean.isCheck());
viewHolder.name.setText(goodBean.getName());
viewHolder.price.setText(goodBean.getPrice()+"");
viewHolder.number.setText(goodBean.getNum()+"");
viewHolder.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goodBean.num++;
if (goodBean.isCheck()){
iShowCart.getPrice();
}
notifyDataSetChanged();//刷新
}
});
viewHolder.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (goodBean.num>0){
goodBean.num--;
if (goodBean.isCheck()){
iShowCart.getPrice();
}
notifyDataSetChanged();//刷新
}
}
});
viewHolder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.check.isChecked()){
goodBean.setCheck(true);
if (dellAll()){
iShowCart.allCheak(num);
} else {
iShowCart.check(num);
}
} else {
goodBean.setCheck(false);
iShowCart.check(num);
}
}
});
}
//按钮是否全选
public boolean dellAll(){
for (int i=0;i<list.size();i++){
if (!list.get(i).isCheck()){
return false;
}
}
return true;
}
class ViewHolder extends RecyclerView.ViewHolder{
CheckBox check;
TextView name;
TextView price;
TextView jian;
TextView number;
TextView jia;
public ViewHolder(View itemView) {
super(itemView);
check = (CheckBox) itemView.findViewById(R.id.check);
name = (TextView) itemView.findViewById(R.id.name);
price = (TextView) itemView.findViewById(R.id.price);
jian = (TextView) itemView.findViewById(R.id.jian);
number = (TextView) itemView.findViewById(R.id.number);
jia = (TextView) itemView.findViewById(R.id.jia);
}
}
@Override
public int getItemCount() {
return list.size();
}
}
//展示购物车
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<GoodBean> list = new ArrayList<>();
private RecyclerView mRv;
private CheckBox mCb;
/**
* 结算
*/
private TextView mNum;
MyAdapter adapter;
int count;
int price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//设置布局管理器
mRv.setLayoutManager(new LinearLayoutManager(this));
for (int i = 0; i < 20; i++) {
list.add(new GoodBean(1, false, "item" + 1, 11));
}
adapter = new MyAdapter(list, MainActivity.this, new IShowCart() {
@Override
public void check(int num) {
mCb.setChecked(false);
aGou();//判断购买的商品
mNum.setText("数量:"+count+" 价格:"+price);
}
@Override
public void allCheak(int num) {
mCb.setChecked(true);
aGou();//判断购买的商品
mNum.setText("数量:"+count+" 价格:"+price);
}
@Override
public void getPrice() {
aGou();//判断购买的商品
mNum.setText("数量:"+count+" 价格:"+price);
}
});
//设置适配器
mRv.setAdapter(adapter);
//全选按钮
mCb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCb.isChecked()){
for (int i=0;i<list.size();i++){
list.get(i).setCheck(true);
}
adapter.notifyDataSetChanged();
} else {
for (int i=0;i<list.size();i++){
list.get(i).setCheck(false);
}
adapter.notifyDataSetChanged();
}
aGou();
mNum.setText("数量:"+count+" 价格:"+price);
}
});
}
//判断有几件为勾选状态
private void aGou(){
count = 0;
price = 0;
for (int i=0;i<list.size();i++){
if (list.get(i).isCheck()){
count+=list.get(i).getNum();
price+=list.get(i).getPrice()+list.get(i).num;
}
}
}
private void initView() {
mRv = (RecyclerView) findViewById(R.id.rv);
mCb = (CheckBox) findViewById(R.id.cb);
mNum = (TextView) findViewById(R.id.num);
}
}