一级购物车

来源:互联网 发布:unity3d导出3dmax模型 编辑:程序博客网 时间:2024/04/26 12:09

//依赖

    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'de.greenrobot:eventbus:3.0.0-beta1'


CheckEvent类


package chengchuankai.baway.com.myapplication;


public class CheckEvent {

    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}


//activity

package chengchuankai.baway.com.myapplication;

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;

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;
        }
    }


}

//MCEvent类

package chengchuankai.baway.com.myapplication;



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;
    }
}


//MyAdapter类


package chengchuankai.baway.com.myapplication;

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;

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 chengchuankai.baway.com.myapplication;

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;


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 + "");
    }

}

//circle_shape.xml

<?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>

//activity_main.xml

<?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">

    <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>


//add_jian_item.xml


<?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.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>

        <chengchuankai.baway.com.myapplication.MyAddDelView
            android:layout_marginLeft="100dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/add_del"></chengchuankai.baway.com.myapplication.MyAddDelView>
    </LinearLayout>

</LinearLayout>


原创粉丝点击