|
| 1 | +package com.example.strivecheng.appdragview; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.v7.app.AppCompatActivity; |
| 5 | +import android.support.v7.widget.DividerItemDecoration; |
| 6 | +import android.support.v7.widget.LinearLayoutManager; |
| 7 | +import android.support.v7.widget.RecyclerView; |
| 8 | +import android.text.Html; |
| 9 | +import android.view.View; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.CheckBox; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import com.chad.library.adapter.base.BaseQuickAdapter; |
| 16 | +import com.example.strivecheng.appdragview.adapter.ShopCarListAdapter; |
| 17 | +import com.example.strivecheng.appdragview.data.GoodsInfo; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * Create by xingcc on 2018/8/9 |
| 24 | + * main function: 购物车界面 |
| 25 | + * |
| 26 | + * @author xingcc |
| 27 | + */ |
| 28 | +public class ShopCarActivity extends AppCompatActivity implements View.OnClickListener { |
| 29 | + |
| 30 | + private Button backBtn; |
| 31 | + private RecyclerView shopListRv; |
| 32 | + private CheckBox allSelectCb; |
| 33 | + private TextView totalTv; |
| 34 | + private Button payMoneyBtn; |
| 35 | + private ArrayList<GoodsInfo> goodsInfos; |
| 36 | + private ShopCarListAdapter shopCarListAdapter; |
| 37 | + /** |
| 38 | + * 记录是否是全选状态 |
| 39 | + */ |
| 40 | + private boolean isAllSelect = true; |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void onCreate(Bundle savedInstanceState) { |
| 44 | + super.onCreate(savedInstanceState); |
| 45 | + setContentView(R.layout.activity_shop_car); |
| 46 | + initView(); |
| 47 | + initListener(); |
| 48 | + initData(); |
| 49 | + } |
| 50 | + |
| 51 | + private void initView() { |
| 52 | + goodsInfos = new ArrayList<>(); |
| 53 | + |
| 54 | + backBtn = findViewById(R.id.back_btn); |
| 55 | + shopListRv = findViewById(R.id.select_shop_list_rv); |
| 56 | + allSelectCb = findViewById(R.id.all_select_cb); |
| 57 | + totalTv = findViewById(R.id.total_tv); |
| 58 | + payMoneyBtn = findViewById(R.id.pay_money_btn); |
| 59 | + |
| 60 | + LinearLayoutManager layoutManager = new LinearLayoutManager(this); |
| 61 | + shopListRv.setLayoutManager(layoutManager); |
| 62 | + shopCarListAdapter = new ShopCarListAdapter(getData()); |
| 63 | + shopListRv.setAdapter(shopCarListAdapter); |
| 64 | + shopListRv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); |
| 65 | + |
| 66 | + //初始化先判断所有商品是否选中 |
| 67 | + setAllSelectCbState(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + private void initListener() { |
| 72 | + backBtn.setOnClickListener(this); |
| 73 | + payMoneyBtn.setOnClickListener(this); |
| 74 | + allSelectCb.setOnClickListener(this); |
| 75 | + shopCarListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() { |
| 76 | + @Override |
| 77 | + public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) { |
| 78 | + GoodsInfo goodsInfo = shopCarListAdapter.getItem(position); |
| 79 | + if (goodsInfo == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + TextView countTv = (TextView) adapter.getViewByPosition(shopListRv, position, R.id.goods_count_et); |
| 83 | + switch (view.getId()) { |
| 84 | + case R.id.sub_goods_iv: |
| 85 | + if (goodsInfo.getCount() <= 1) { |
| 86 | + Toast.makeText(ShopCarActivity.this, "不能再少啦!", Toast.LENGTH_SHORT).show(); |
| 87 | + return; |
| 88 | + } |
| 89 | + goodsInfo.setCount(goodsInfo.getCount() - 1); |
| 90 | + countTv.setText(goodsInfo.getCount() + ""); |
| 91 | + if (goodsInfo.isSelect()) { |
| 92 | + refreshTotal(); |
| 93 | + } |
| 94 | + break; |
| 95 | + case R.id.add_goods_iv: |
| 96 | + goodsInfo.setCount(goodsInfo.getCount() + 1); |
| 97 | + countTv.setText(goodsInfo.getCount() + ""); |
| 98 | + if (goodsInfo.isSelect()) { |
| 99 | + refreshTotal(); |
| 100 | + } |
| 101 | + break; |
| 102 | + case R.id.delete_btn: |
| 103 | + goodsInfos.remove(position); |
| 104 | + adapter.notifyItemRemoved(position); |
| 105 | + refreshTotal(); |
| 106 | + break; |
| 107 | + case R.id.select_cb: |
| 108 | + goodsInfo.setSelect(!goodsInfo.isSelect()); |
| 109 | + refreshTotal(); |
| 110 | + setAllSelectCbState(); |
| 111 | + break; |
| 112 | + default: |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | +// allSelectCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { |
| 118 | +// @Override |
| 119 | +// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 120 | +// if (buttonView.isEnabled()) { |
| 121 | +// allSelectGoods(isChecked); |
| 122 | +// shopCarListAdapter.notifyDataSetChanged(); |
| 123 | +// refreshTotal(); |
| 124 | +// } |
| 125 | +// } |
| 126 | +// }); |
| 127 | + } |
| 128 | + |
| 129 | + private void initData() { |
| 130 | + refreshTotal(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 刷新总结 |
| 135 | + */ |
| 136 | + private void refreshTotal() { |
| 137 | + getTotal(); |
| 138 | + String content = "合计支付:<font color=\"#33000000\" ><big>" + getTotal() + "</big></font> "; |
| 139 | + totalTv.setText(Html.fromHtml(content)); |
| 140 | +// totalTv.setText("" + getTotal()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * 获取要支付的总价 |
| 145 | + */ |
| 146 | + private double getTotal() { |
| 147 | + double total = 0; |
| 148 | + for (GoodsInfo goods : goodsInfos) { |
| 149 | + if (goods.isSelect()) { |
| 150 | + total = total + goods.getPrice() * goods.getCount(); |
| 151 | + } |
| 152 | + } |
| 153 | + return total; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void onClick(View v) { |
| 158 | + switch (v.getId()) { |
| 159 | + case R.id.back_btn: |
| 160 | + finish(); |
| 161 | + break; |
| 162 | + case R.id.pay_money_btn: |
| 163 | + Toast.makeText(this, "您一共消费了" + getTotal() + "元!", Toast.LENGTH_SHORT).show(); |
| 164 | + break; |
| 165 | + case R.id.all_select_cb: |
| 166 | + boolean isChecked = ((CheckBox) v).isChecked(); |
| 167 | + allSelectGoods(isChecked); |
| 168 | + shopCarListAdapter.notifyDataSetChanged(); |
| 169 | + refreshTotal(); |
| 170 | + break; |
| 171 | + default: |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * 判断是否所有商品全为选中 |
| 177 | + */ |
| 178 | + private boolean allGoodsIsAllSelect() { |
| 179 | + isAllSelect = true; |
| 180 | + for (GoodsInfo goods : goodsInfos) { |
| 181 | + isAllSelect = isAllSelect && goods.isSelect(); |
| 182 | + } |
| 183 | + return isAllSelect; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * 设置全选按钮的状态 |
| 188 | + */ |
| 189 | + private void setAllSelectCbState() { |
| 190 | + allGoodsIsAllSelect(); |
| 191 | + allSelectCb.setChecked(isAllSelect); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * 全选或者取消全选商品 |
| 196 | + */ |
| 197 | + private void allSelectGoods(boolean isSelect) { |
| 198 | + for (GoodsInfo goods : goodsInfos) { |
| 199 | + goods.setSelect(isSelect); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + private List<GoodsInfo> getData() { |
| 204 | + for (int i = 0; i < 5; i++) { |
| 205 | + GoodsInfo goodsInfo = new GoodsInfo(); |
| 206 | + goodsInfo.setCount(1); |
| 207 | + goodsInfo.setName("这是最牛逼的手机 iPhone X"); |
| 208 | + goodsInfo.setPrice(10); |
| 209 | + goodsInfo.setType("深空灰"); |
| 210 | + goodsInfo.setSelect(false); |
| 211 | + goodsInfos.add(goodsInfo); |
| 212 | + } |
| 213 | + return goodsInfos; |
| 214 | + } |
| 215 | + |
| 216 | +} |
0 commit comments