一级列表购物车完整版

----------------------------MainActivity------------------------------------------

package com.example.shopcarexam;

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


}

-------------------MyAdapter---------------------------------------

package com.example.shopcarexam;

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;

/**
 * Created by hh on 2017/10/19.
 */

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 com.example.shopcarexam;

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;

/**
 * Created by hh on 2017/10/25.
 */

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

}

--------------------------Bean--------------------------------------

package com.example.shopcarexam;

/**
 * Created by hh on 2017/10/19.
 */

public class Bean {
    private boolean checked;
    private int price;
    private String title;

    public Bean(boolean checked, int price, String title) {
        this.checked = checked;
        this.price = price;
        this.title = title;
    }

    public boolean isChecked() {
        return checked;
    }

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

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

---------------------------------CheckEvent--------------------------------------------------

package com.example.shopcarexam;

/**
 * Created by hh on 2017/10/22.
 */

public class CheckEvent {

    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

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

package com.example.shopcarexam;

/**
 * Created by hh on 2017/10/22.
 */

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

--------------------MainActivity布局文件-----------------------------------------

<?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"
    tools:context="com.example.shopcarexam.MainActivity">

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


----------------------------------jia_jian_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="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布局------------------------------------------------

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

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

</LinearLayout>

--------------------------------------drawable文件 circle_shape-----------------------------------------------

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值