. 购物车页面可实现商品数量的增减、商品的删除以及总价格计算功能。用Android实现写出代码
时间: 2024-03-04 14:49:06 浏览: 84
Android 购物车加减功能的实现代码
5星 · 资源好评率100%
好的,以下是一个简单的购物车页面的实现代码:
布局文件 cart_layout.xml:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/cart_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/total_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="总价格:0元" />
<Button
android:id="@+id/checkout_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="结算" />
</LinearLayout>
```
Java代码:
```
public class CartActivity extends AppCompatActivity {
private ListView cartList;
private TextView totalPrice;
private Button checkoutBtn;
private List<CartItem> itemList = new ArrayList<>();
private CartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_layout);
cartList = findViewById(R.id.cart_list);
totalPrice = findViewById(R.id.total_price);
checkoutBtn = findViewById(R.id.checkout_btn);
// 初始化商品列表
itemList.add(new CartItem("商品1", 10.0, 1));
itemList.add(new CartItem("商品2", 20.0, 2));
itemList.add(new CartItem("商品3", 30.0, 3));
adapter = new CartAdapter(this, itemList);
cartList.setAdapter(adapter);
// 计算总价格
double total = 0.0;
for (CartItem item : itemList) {
total += item.getPrice() * item.getNum();
}
totalPrice.setText("总价格:" + total + "元");
// 增减商品数量
adapter.setOnNumChangeListener(new CartAdapter.OnNumChangeListener() {
@Override
public void onNumChange(int position, int num) {
itemList.get(position).setNum(num);
adapter.notifyDataSetChanged();
// 重新计算总价格
double total = 0.0;
for (CartItem item : itemList) {
total += item.getPrice() * item.getNum();
}
totalPrice.setText("总价格:" + total + "元");
}
});
// 删除商品
adapter.setOnDeleteListener(new CartAdapter.OnDeleteListener() {
@Override
public void onDelete(int position) {
itemList.remove(position);
adapter.notifyDataSetChanged();
// 重新计算总价格
double total = 0.0;
for (CartItem item : itemList) {
total += item.getPrice() * item.getNum();
}
totalPrice.setText("总价格:" + total + "元");
}
});
// 结算
checkoutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 进行结算操作
}
});
}
}
class CartAdapter extends BaseAdapter {
private Context context;
private List<CartItem> itemList;
private LayoutInflater inflater;
// 商品数量变化监听器
private OnNumChangeListener onNumChangeListener;
// 商品删除监听器
private OnDeleteListener onDeleteListener;
public CartAdapter(Context context, List<CartItem> itemList) {
this.context = context;
this.itemList = itemList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.cart_item_layout, null);
holder = new ViewHolder();
holder.name = convertView.findViewById(R.id.item_name);
holder.price = convertView.findViewById(R.id.item_price);
holder.num = convertView.findViewById(R.id.item_num);
holder.addBtn = convertView.findViewById(R.id.add_btn);
holder.minusBtn = convertView.findViewById(R.id.minus_btn);
holder.deleteBtn = convertView.findViewById(R.id.delete_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final CartItem item = itemList.get(position);
holder.name.setText(item.getName());
holder.price.setText("单价:" + item.getPrice() + "元");
holder.num.setText(item.getNum() + "");
holder.addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = item.getNum() + 1;
item.setNum(num);
notifyDataSetChanged();
if (onNumChangeListener != null) {
onNumChangeListener.onNumChange(position, num);
}
}
});
holder.minusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = item.getNum() - 1;
if (num < 1) {
num = 1;
}
item.setNum(num);
notifyDataSetChanged();
if (onNumChangeListener != null) {
onNumChangeListener.onNumChange(position, num);
}
}
});
holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onDeleteListener != null) {
onDeleteListener.onDelete(position);
}
}
});
return convertView;
}
public void setOnNumChangeListener(OnNumChangeListener listener) {
this.onNumChangeListener = listener;
}
public void setOnDeleteListener(OnDeleteListener listener) {
this.onDeleteListener = listener;
}
interface OnNumChangeListener {
void onNumChange(int position, int num);
}
interface OnDeleteListener {
void onDelete(int position);
}
static class ViewHolder {
TextView name;
TextView price;
TextView num;
Button addBtn;
Button minusBtn;
Button deleteBtn;
}
}
class CartItem {
private String name;
private double price;
private int num;
public CartItem(String name, double price, int num) {
this.name = name;
this.price = price;
this.num = num;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
```
这个购物车页面实现了商品数量的增减、商品的删除以及总价格计算功能。购物车中的商品列表使用 ListView 控件展示,每个商品项中包含商品名称、单价、数量、增加数量按钮、减少数量按钮和删除按钮。购物车页面的总价格通过计算所有商品的数量和单价得出。购物车页面的结算按钮还未实现具体的结算操作,需要根据实际情况进行编写。
阅读全文