Android實(shí)現(xiàn)淘寶購物車
本文實(shí)例為大家分享了Android實(shí)現(xiàn)淘寶購物車的具體代碼,供大家參考,具體內(nèi)容如下
功能基本和淘寶購物車一樣,商品按照店鋪分類顯示,全選,反選,選中商品數(shù)量變化,總價(jià)隨之變化。效果圖

思路:店鋪和商品都增加一個(gè)select屬性,列表的CheckBox選擇或未選中狀態(tài)改變同時(shí)設(shè)置店鋪和商品的select屬性,每次CheckBox狀態(tài)改變設(shè)置select的值等于cb.isChecked()
購物車頁面布局文件activity_shopping_car
<?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=".ui.activity.ShoppingCarActivity">
<com.hjq.bar.TitleBar
android:id="@+id/titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rightTitle="管理"
app:title="購物車" />
<com.qiyetec.flyingsnail.widget.HintLayout
android:id="@+id/hintLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smartRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</com.qiyetec.flyingsnail.widget.HintLayout>
<LinearLayout
android:id="@+id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/ali_auth_space_10"
android:background="#fff"
android:gravity="center_vertical"
android:padding="15dp">
<CheckBox
android:id="@+id/cb_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/selector_checkbox_green"
android:text="全選" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="合計(jì):"
android:textColor="#333"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="¥0.00"
android:textColor="#DA3527"
android:textSize="24sp" />
<TextView
android:id="@+id/tv_buy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@drawable/shape_red10"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"
android:text="結(jié)算"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_stroke_red15"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:paddingBottom="5dp"
android:text="刪除"
android:textColor="#DA3527"
android:textSize="18sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
Java代碼
public class ShoppingCarActivity extends MyActivity implements StatusAction {
@BindView(R.id.titleBar)
TitleBar titleBar;
@BindView(R.id.rv)
RecyclerView rv;
@BindView(R.id.hintLayout)
HintLayout hintLayout;
@BindView(R.id.ll_bottom)
LinearLayout ll_bottom;
@BindView(R.id.ll)
LinearLayout ll;
@BindView(R.id.tv_total_price)
TextView tv_total_price;
@BindView(R.id.tv_del)
TextView tv_del;
@BindView(R.id.cb_all)
CheckBox cb_all;
@BindView(R.id.smartRefreshLayout)
SmartRefreshLayout smartRefreshLayout;
private ShoppingCarAdapter shoppingCarAdapter;
private int total_page, page = 1;
@Override
protected int getLayoutId() {
return R.layout.activity_shopping_car;
}
@Override
protected void initView() {
setOnClickListener(R.id.tv_buy, R.id.tv_del);
}
@Override
protected void initData() {
EventBus.getDefault().register(this);
//網(wǎng)絡(luò)請求 獲取購物車數(shù)據(jù)
getCarData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
rv.setLayoutManager(linearLayoutManager);
shoppingCarAdapter = new ShoppingCarAdapter(this);
//商品選中價(jià)格改變回調(diào)
shoppingCarAdapter.setOnPriceChangeListener(new ShoppingCarAdapter.OnPriceChangeListener() {
@Override
public void onClick(boolean allSelect, String totalPrice) {
cb_all.setChecked(allSelect);
tv_total_price.setText("¥" + totalPrice);
}
});
rv.setAdapter(shoppingCarAdapter);
//全選 反選
cb_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (shoppingCarAdapter != null && shoppingCarAdapter.getData() != null) {
for (int i = 0; i < shoppingCarAdapter.getData().size(); i++) {
shoppingCarAdapter.getData().get(i).setSelect(cb_all.isChecked());
for (int j = 0; j < shoppingCarAdapter.getData().get(i).getItem_data().size(); j++) {
shoppingCarAdapter.getData().get(i).getItem_data().get(j).setSelect(cb_all.isChecked());
}
}
shoppingCarAdapter.notifyDataSetChanged();
shoppingCarAdapter.calculatePrice();
}
}
});
smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
page = 1;
getCarData();
refreshLayout.finishRefresh();
}
}).setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
if (page <= total_page) {
getCarData();
}
refreshLayout.finishLoadMore();
}
});
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onGetMessage(MessageWrap message) {
if (message.message.equals("refresh_car")) {
getCarData();
}
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
public void onRightClick(View v) {
if (titleBar.getRightTitle().equals("管理")) {
titleBar.setRightTitle("完成");
ll.setVisibility(View.GONE);
tv_del.setVisibility(View.VISIBLE);
} else if (titleBar.getRightTitle().equals("完成")) {
titleBar.setRightTitle("管理");
ll.setVisibility(View.VISIBLE);
tv_del.setVisibility(View.GONE);
}
}
@SingleClick
@Override
public void onClick(View v) {
switch (v.getId()) {
//點(diǎn)擊結(jié)算
case R.id.tv_buy:
if (shoppingCarAdapter != null && shoppingCarAdapter.getData() != null) {
JSONArray ja = new JSONArray();
for (ShoppingCarBean shoppingCarBean : shoppingCarAdapter.getData()) {
JSONArray array = new JSONArray();
JSONObject object = null;
for (ShoppingCarBean.ItemDataBean itemDataBean : shoppingCarBean.getItem_data())
if (itemDataBean.isSelect()) {
object = new JSONObject();
if (StringUtils.isNotNullOrEmpty(shoppingCarBean.getShop().getShop_id()))
object.put("shop_id", shoppingCarBean.getShop().getShop_id());
else
object.put("shop_id", 1);
JSONObject object1 = new JSONObject();
object1.put("item_id", itemDataBean.getItem_id());
object1.put("count", itemDataBean.getCount());
object1.put("cart_id", itemDataBean.getCart_id());
object1.put("sku_id", itemDataBean.getSku_id());
array.add(object1);
object.put("items", array);
}
if (object != null)
ja.add(object);
}
// Log.i("logger", "onClick: " + JSONObject.toJSONString(ja));
if (ja != null && ja.size() > 0) {
//提交
confirmOrder(ja);
} else
toast("請選擇商品");
}
break;
case R.id.tv_del:
if (shoppingCarAdapter != null && shoppingCarAdapter.getData() != null) {
List<String> list = new ArrayList<>();
for (ShoppingCarBean shoppingCarBean : shoppingCarAdapter.getData()) {
for (ShoppingCarBean.ItemDataBean itemDataBean : shoppingCarBean.getItem_data())
if (itemDataBean.isSelect()) {
list.add(itemDataBean.getCart_id());
}
}
delCart(list);
}
break;
}
}
@Override
public HintLayout getHintLayout() {
return hintLayout;
}
}
adapter代碼
public final class ShoppingCarAdapter extends MyAdapter<ShoppingCarBean> {
public ShoppingCarAdapter(Context context) {
super(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder();
}
final class ViewHolder extends MyAdapter.ViewHolder {
@BindView(R.id.cb)
CheckBox cb;
@BindView(R.id.iv_shopicon)
ImageView iv_shopicon;
@BindView(R.id.tv_shopname)
TextView tv_shopname;
@BindView(R.id.ll)
LinearLayout linearLayout;
ViewHolder() {
super(R.layout.item_car);
}
@Override
public void onBindView(int position) {
ShoppingCarBean bean = getItem(position);
tv_shopname.setText(bean.getShop().getShop_name());
cb.setChecked(bean.isSelect());
//每次添加前先移除所有布局
linearLayout.removeAllViews();
if (bean.getItem_data() != null) {
//動(dòng)態(tài)添加商品列表
for (int i = 0; i < bean.getItem_data().size(); i++) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_item_car, null);
CheckBox c_cb = view.findViewById(R.id.cb);
ImageView iv_cover = view.findViewById(R.id.iv);
TextView tv_title = view.findViewById(R.id.tv_title);
TextView tv_guige = view.findViewById(R.id.tv_guige);
TextView tv_price = view.findViewById(R.id.tv_price);
TextView tv_count = view.findViewById(R.id.tv_count);
TextView tv_des = view.findViewById(R.id.tv_des);
TextView tv_add = view.findViewById(R.id.tv_add);
tv_title.setText(bean.getItem_data().get(i).getTitle());
tv_price.setText("¥" + bean.getItem_data().get(i).getZk_price());
tv_count.setText("" + bean.getItem_data().get(i).getCount());
tv_guige.setText(bean.getItem_data().get(i).getSku());
Glide.with(getContext()).load(bean.getItem_data().get(i).getPic())
.transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics())))
.into(iv_cover);
if (bean.isSelect()) {
c_cb.setChecked(true);
} else {
c_cb.setChecked(bean.getItem_data().get(i).isSelect());
}
int finalI = i;
tv_des.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bean.getItem_data().get(finalI).getCount() > 1) {
bean.getItem_data().get(finalI).setCount(bean.getItem_data().get(finalI).getCount()-1);
tv_count.setText(bean.getItem_data().get(finalI).getCount()+"");
calculatePrice();
} else
ToastUtils.show("不能再少了哦~");
}
});
tv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bean.getItem_data().get(finalI).setCount(bean.getItem_data().get(finalI).getCount()+1);
tv_count.setText(bean.getItem_data().get(finalI).getCount()+"");
calculatePrice(); }
});
c_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean select = true;
bean.getItem_data().get(finalI).setSelect(c_cb.isChecked());
for (int j = 0; j < bean.getItem_data().size(); j++) {
if (bean.getItem_data().get(j).isSelect() == false) {
select = false;
break;
}
}
bean.setSelect(select);
notifyDataSetChanged();
calculatePrice();
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), ShoppingDetailActivity.class);
intent.putExtra("item_id", bean.getItem_data().get(finalI).getItem_id());
startActivity(intent);
}
});
linearLayout.addView(view);
}
}
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bean.setSelect(cb.isChecked());
for (int j = 0; j < bean.getItem_data().size(); j++) {
bean.getItem_data().get(j).setSelect(cb.isChecked());
}
notifyDataSetChanged();
calculatePrice();
}
});
}
}
public interface OnPriceChangeListener {
void onClick(boolean allSelect, String totalPrice);
}
private OnPriceChangeListener onPriceChangeListener;
public void setOnPriceChangeListener(OnPriceChangeListener onPriceChangeListener) {
this.onPriceChangeListener = onPriceChangeListener;
}
public void calculatePrice() {
if (onPriceChangeListener != null) {
BigDecimal total = new BigDecimal("0.00");
boolean allSelect = true;
List<ShoppingCarBean> list = getData();
for (int i = 0; i < getItemCount(); i++) {
if (list.get(i).isSelect() == false)
allSelect = false;
for (int j = 0; j < list.get(i).getItem_data().size(); j++) {
if (list.get(i).getItem_data().get(j).isSelect()) {
BigDecimal multiply = new BigDecimal(list.get(i).getItem_data().get(j).getZk_price()).multiply(new BigDecimal(list.get(i).getItem_data().get(j).getCount()));
BigDecimal price = new BigDecimal(multiply + "");
total = total.add(price);
}
}
}
onPriceChangeListener.onClick(allSelect, total + "");
}
}
}
RecyclerView條目布局item_car
<?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="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:background="@drawable/shape_white5"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/selector_checkbox_green" />
<ImageView
android:id="@+id/iv_shopicon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/tv_shopname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#333"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
商品條目布局layout_item_car
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="5dp">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:button="@drawable/selector_checkbox_green" />
<ImageView
android:id="@+id/iv"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/cb" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/iv"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#333"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_guige"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@id/iv"
android:background="@drawable/shape_gray5"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:textColor="#C1C1C1"
android:textSize="10sp" />
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_guige"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/iv"
android:textColor="#DA3527"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_below="@id/tv_guige"
android:layout_alignParentRight="true"
android:layout_marginTop="8dp"
android:layout_marginBottom="5dp"
android:background="@drawable/stroke_gray2">
<TextView
android:id="@+id/tv_des"
android:layout_width="25dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="-"
android:textColor="#cccccc" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#cccccc" />
<TextView
android:id="@+id/tv_count"
android:layout_width="30dp"
android:layout_height="match_parent"
android:gravity="center" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#cccccc" />
<TextView
android:id="@+id/tv_add"
android:layout_width="25dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="+"
android:textColor="#cccccc" />
</LinearLayout>
</RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter本地存儲(chǔ)之基本的鍵值對存儲(chǔ)詳解
在原生的?Android?或?iOS?中,都提供了基本的鍵值對存儲(chǔ)方式,在?Flutter?中,提供了?shared_preferences?這個(gè)插件來實(shí)現(xiàn)本地鍵值對數(shù)據(jù)存儲(chǔ),本文就來和大家簡單聊聊吧2023-03-03
Android使用TransitionDrawable漸變切換多張圖片
這篇文章主要為大家詳細(xì)介紹了Android使用TransitionDrawable漸變切換多張圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Windows下快速搭建安卓開發(fā)環(huán)境Android studio
這篇文章主要介紹了Windows下快速搭建安卓開發(fā)環(huán)境Android studio的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07
Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼)
這篇文章主要介紹了Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android App實(shí)現(xiàn)應(yīng)用內(nèi)部自動(dòng)更新的最基本方法示例
這篇文章主要介紹了實(shí)現(xiàn)Android App內(nèi)部自動(dòng)更新的最基本方法示例,包括IIS服務(wù)器端的簡單布置,需要的朋友可以參考下2016-03-03
Android 動(dòng)畫(View動(dòng)畫,幀動(dòng)畫,屬性動(dòng)畫)詳細(xì)介紹
這篇文章主要介紹了Android View動(dòng)畫、幀動(dòng)畫和屬性動(dòng)畫詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android View滑動(dòng)的實(shí)現(xiàn)分析示例
View滑動(dòng)是Android實(shí)現(xiàn)自定義控件的基礎(chǔ),同時(shí)在開發(fā)中難免會(huì)遇到View的滑動(dòng)處理,其實(shí)不管是那種滑動(dòng)方法,基本思路是類似的;當(dāng)點(diǎn)擊事件傳到View時(shí),系統(tǒng)記下觸摸點(diǎn)的坐標(biāo),手指移動(dòng)時(shí)系統(tǒng)記下移動(dòng)后的左邊并算出偏移量,通過偏移量來修改View的坐標(biāo)2022-08-08
Android利用shape實(shí)現(xiàn)各種簡單的形狀
這篇文章主要給大家介紹了關(guān)于Android中利用shape實(shí)現(xiàn)各種簡單的形狀的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。2017-05-05

