android ------ RecyclerView imitates Taobao shopping cart

Shopping cart is often used in e-commerce projects. There are many projects with different interfaces. Choose one to talk about it. RecyclerView imita...

Shopping cart is often used in e-commerce projects. There are many projects with different interfaces. Choose one to talk about it.


RecyclerView imitates the function of Taobao shopping cart (delete and select goods, calculate goods, select, select all and invert selection, increase and decrease of goods quantity, etc.)

Look at the renderings:

Activity Code:

/***** * RecyclerView Imitating the function of Taobao shopping cart * * Delete and select goods, calculate goods, select, select all and invert selection, add and subtract goods quantity, etc * */ public class MainActivity extends AppCompatActivity { private RecyclerView rvNestDemo; private CartAdapter cartAdapter; CartInfo cartInfo; double price; int num; TextView cartNum; TextView cartMoney; Button cartShoppMoular; CheckBox checkBox; private TextView btnDelete; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rvNestDemo = (RecyclerView) findViewById(R.id.rv_nest_demo); cartNum = findViewById(R.id.cart_num); cartMoney = findViewById(R.id.cart_money); cartShoppMoular = findViewById(R.id.cart_shopp_moular); cartShoppMoular.setOnClickListener(new OnClickListener()); checkBox = findViewById(R.id.cbx_quanx_check); checkBox.setOnClickListener(new OnClickListener()); btnDelete = (TextView) findViewById(R.id.btn_delete); initView(); } private void initView() { btnDelete.setOnClickListener(new OnClickListener()); showData(); rvNestDemo.setLayoutManager(new LinearLayoutManager(this)); DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); itemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.line_divider_inset)); rvNestDemo.addItemDecoration(itemDecoration); cartAdapter = new CartAdapter(this, cartInfo.getData()); rvNestDemo.setAdapter(cartAdapter); showExpandData(); } private void showExpandData() { /** * All election */ cartAdapter.setOnItemClickListener(new OnViewItemClickListener() { @Override public void onItemClick(boolean isFlang, View view, int position) { cartInfo.getData().get(position).setIscheck(isFlang); int length = cartInfo.getData().get(position).getItems().size(); for (int i = 0; i < length; i++) { cartInfo.getData().get(position).getItems().get(i).setIscheck(isFlang); } cartAdapter.notifyDataSetChanged(); showCommodityCalculation(); } }); /** * Calculate the price */ cartAdapter.setOnItemMoneyClickListener(new OnItemMoneyClickListener() { @Override public void onItemClick(View view, int position) { showCommodityCalculation(); } }); } /*** * Calculate quantity and price of goods */ private void showCommodityCalculation() { price = 0; num = 0; for (int i = 0; i < cartInfo.getData().size(); i++) { for (int j = 0; j < cartInfo.getData().get(i).getItems().size(); j++) { if (cartInfo.getData().get(i).getItems().get(j).ischeck()) { price += Double.valueOf((cartInfo.getData().get(i).getItems().get(j).getNum() * Double.valueOf(cartInfo.getData().get(i).getItems().get(j).getPrice()))); num++; } else { checkBox.setChecked(false); } } } if (price == 0.0) { cartNum.setText("0 items in total"); cartMoney.setText("¥ 0.0"); return; } try { String money = String.valueOf(price); cartNum.setText("common" + num + "Commodity"); if (money.substring(money.indexOf("."), money.length()).length() > 2) { cartMoney.setText("¥ " + money.substring(0, (money.indexOf(".") + 3))); return; } cartMoney.setText("¥ " + money.substring(0, (money.indexOf(".") + 2))); } catch (Exception e) { e.printStackTrace(); } } private void showData() { cartInfo = JSON.parseObject(JSONDATA(), CartInfo.class); } private class OnClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { //All or not case R.id.cbx_quanx_check: if (checkBox.isChecked()) { int length = cartInfo.getData().size(); for (int i = 0; i < length; i++) { cartInfo.getData().get(i).setIscheck(true); int lengthn = cartInfo.getData().get(i).getItems().size(); for (int j = 0; j < lengthn; j++) { cartInfo.getData().get(i).getItems().get(j).setIscheck(true); } } } else { int length = cartInfo.getData().size(); for (int i = 0; i < length; i++) { cartInfo.getData().get(i).setIscheck(false); int lengthn = cartInfo.getData().get(i).getItems().size(); for (int j = 0; j < lengthn; j++) { cartInfo.getData().get(i).getItems().get(j).setIscheck(false); } } } cartAdapter.notifyDataSetChanged(); showCommodityCalculation(); break; case R.id.btn_delete: //Delete selected item cartAdapter.removeChecked(); showCommodityCalculation(); break; case R.id.cart_shopp_moular: Toast.makeText(MainActivity.this,"place order: "+cartMoney.getText().toString()+"element",Toast.LENGTH_LONG).show(); break; } } }

This is the second shopping cart I wrote. It has all the basic functions.

If you need help, please refer to it.

Code download: https://github.com/DickyQie/android-shoppingcart/tree/tb-cart/

4 December 2019, 18:38 | Views: 8133

Add new comment

For adding a comment, please log in
or create account

0 comments