仿购物车

来源:互联网 发布:淘宝出版物许可证 代办 编辑:程序博客网 时间:2024/06/07 15:31

--------------------------------------------------------------------------------------------------------

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

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

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

public class MyAdapter extends RecyclerView.Adapter {    private Context context;    private List<GWCbean> list;    public MyAdapter(Context context, List<GWCbean> list) {        this.context = context;        this.list = list;    }    @Override    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {        View view=View.inflate(context,R.layout.rcview_item,null);        MyViewHolder holder = new MyViewHolder(view);        return holder;    }    @Override    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {        if(holder instanceof MyViewHolder){            ((MyViewHolder) holder).cb_goods.setChecked(list.get(position).isB());            ((MyViewHolder) holder).tv_name.setText(list.get(position).getName());            ((MyViewHolder) holder).tv_price.setText(list.get(position).getPrice()+"");            ((MyViewHolder) holder).cb_goods.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    list.get(position).setB(!list.get(position).isB());                    boolean b=true;                    for (int i = 0; i < list.size(); i++) {                        if(!list.get(i).isB()){                            b=false;                        }                    }                    EventBus.getDefault().post(new EventBean(b,list));                    notifyDataSetChanged();                }            });        }    }    @Override    public int getItemCount() {        return list.size();    }    class MyViewHolder extends RecyclerView.ViewHolder{        public CheckBox cb_goods;        public TextView tv_name;        public TextView tv_price;        public MyViewHolder(View itemView) {            super(itemView);            cb_goods=itemView.findViewById(R.id.cb_goods);            tv_name=itemView.findViewById(R.id.tv_name);            tv_price=itemView.findViewById(R.id.tv_price);        }    }}
--------------------布局

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

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

原创粉丝点击