二级列表仿购物车

来源:互联网 发布:java生成安装包 编辑:程序博客网 时间:2024/04/29 13:23

//主布局

<?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.happy.monigwc.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp">
        <CheckBox
            android:id="@+id/cb_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:text="全选"/>
        <TextView
            android:id="@+id/tv_heji"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp"/>
        <Button
            android:id="@+id/btn_buy"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:textColor="#fff"
            android:textStyle="bold"
            android:text="付款(0)"
            android:background="#f00"/>
    </LinearLayout>

</LinearLayout>



   //item布局

<?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="60dp"
    tools:context="com.happy.monigwc.MainActivity">


    <CheckBox
        android:id="@+id/cb_goods"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp" />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:background="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="5dp"/>
        <TextView
            android:id="@+id/tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"/>

    </LinearLayout>

</LinearLayout>

//MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView rcview;
    private CheckBox cb_all;
    private TextView tv_heji;
    private Button btn_buy;
    private List<GWCbean> datas;

    private int count=0;
    private int nums=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        rcview = (RecyclerView) findViewById(R.id.rcview);
        cb_all = (CheckBox) findViewById(R.id.cb_all);
        tv_heji = (TextView) findViewById(R.id.tv_heji);
        btn_buy = (Button) findViewById(R.id.btn_buy);


        EventBus.getDefault().register(this);


        //实例化一个集合做为recyclerview的模拟数据

        datas = new ArrayList<>();

        for (int i = 0; i < 20; i++) {

            datas.add(new GWCbean("我是第"+i+"条数据",700+i,false));
        }

        rcview.setLayoutManager(new LinearLayoutManager(this));

        MyAdapter adapter = new MyAdapter(this, datas);
        rcview.setAdapter(adapter);

        cb_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                boolean b=cb_all.isChecked();

                for (int i = 0; i < datas.size(); i++) {
                    datas.get(i).setB(b);

                    if(b){

                        count++;
                        nums+=datas.get(i).getPrice();
                    }


                }

                btn_buy.setText("付款("+count+")");
                tv_heji.setText(nums+"元");

                nums=0;
                count=0;
                rcview.setLayoutManager(new LinearLayoutManager(MainActivity.this));

                MyAdapter adapter = new MyAdapter(MainActivity.this, datas);
                rcview.setAdapter(adapter);
            }
        });

    }

    @Subscribe
    public void onEvent(EventBean bean){

        boolean b=bean.is_all();
        datas=bean.getList();

        cb_all.setChecked(b);


        for (int i = 0; i < datas.size(); i++) {

            if(datas.get(i).isB()){
                count++;
                nums+=datas.get(i).getPrice();
            }
        }

        btn_buy.setText("付款("+count+")");
        tv_heji.setText(nums+"元");

        nums=0;
        count=0;
        rcview.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        MyAdapter adapter = new MyAdapter(MainActivity.this, datas);
        rcview.setAdapter(adapter);



    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

//GWBean

public class GWCbean {

    private String name;
    private int price;
    private boolean b;

    public GWCbean(String name, int price, boolean b) {
        this.name = name;
        this.price = price;
        this.b = b;
    }

    @Override
    public String toString() {
        return "GWCbean{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", b=" + b +
                '}';
    }

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


//EvenBean

public class EventBean {


    private boolean is_all;
    private List<GWCbean> list;

    public EventBean(boolean is_all, List<GWCbean> list) {
        this.is_all = is_all;
        this.list = list;
    }

    @Override
    public String toString() {
        return "EventBean{" +
                "is_all=" + is_all +
                ", list=" + list +
                '}';
    }

    public boolean is_all() {
        return is_all;
    }

    public void setIs_all(boolean is_all) {
        this.is_all = is_all;
    }

    public List<GWCbean> getList() {
        return list;
    }

    public void setList(List<GWCbean> list) {
        this.list = list;
    }

原创粉丝点击