1. 点击某一个商品进入本商品的详情页,详情页数据包括商品图片、商品名称、商品价格、该商品的详细介绍。 2. 商品详情页底部包含一个“购买”按钮,点击购买后,跳转到购物车页面。 3. 购物车页面可实现商品数量的增减、商品的删除以及总价格计算功能。用Android实现写出代码
时间: 2024-05-18 12:16:21 浏览: 272
好的,以下是一个简单的代码示例,可以实现您所描述的功能。请注意,这只是一个示例,您需要根据自己的需求进行修改和扩展。
首先,我们需要在布局文件中定义商品列表和商品详情的布局。例如:
activity_product_list.xml
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_product_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
activity_product_detail.xml
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_product_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/tv_product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_product_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_product_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp" />
<Button
android:id="@+id/btn_add_to_cart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="购买" />
</LinearLayout>
```
接下来,我们需要在代码中实现上述布局的逻辑。我们可以使用一个Product类来表示商品,包括商品名称、价格、图片和详细描述等属性。我们还需要实现一个ProductAdapter类来管理商品列表,并使用Intent来传递所选商品的详细信息。例如:
Product.java
```java
public class Product {
private String name;
private double price;
private int imageResId;
private String description;
public Product(String name, double price, int imageResId, String description) {
this.name = name;
this.price = price;
this.imageResId = imageResId;
this.description = description;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getImageResId() {
return imageResId;
}
public String getDescription() {
return description;
}
}
```
ProductAdapter.java
```java
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private List<Product> productList;
public ProductAdapter(List<Product> productList) {
this.productList = productList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Product product = productList.get(position);
holder.ivProductImage.setImageResource(product.getImageResId());
holder.tvProductName.setText(product.getName());
holder.tvProductPrice.setText(String.format(Locale.getDefault(), "%.2f 元", product.getPrice()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ProductDetailActivity.class);
intent.putExtra("product", product);
view.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView ivProductImage;
public TextView tvProductName;
public TextView tvProductPrice;
public ViewHolder(View itemView) {
super(itemView);
ivProductImage = itemView.findViewById(R.id.iv_product_image);
tvProductName = itemView.findViewById(R.id.tv_product_name);
tvProductPrice = itemView.findViewById(R.id.tv_product_price);
}
}
}
```
在商品详情活动中,我们可以使用Intent从商品列表活动中获取所选商品的详细信息,并将其显示在界面上。我们还需要实现一个购买按钮的点击事件,将所选商品添加到购物车中,并跳转到购物车活动。例如:
ProductDetailActivity.java
```java
public class ProductDetailActivity extends AppCompatActivity {
private ImageView ivProductImage;
private TextView tvProductName;
private TextView tvProductPrice;
private TextView tvProductDescription;
private Button btnAddToCart;
private Product product;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
ivProductImage = findViewById(R.id.iv_product_image);
tvProductName = findViewById(R.id.tv_product_name);
tvProductPrice = findViewById(R.id.tv_product_price);
tvProductDescription = findViewById(R.id.tv_product_description);
btnAddToCart = findViewById(R.id.btn_add_to_cart);
product = getIntent().getParcelableExtra("product");
if (product != null) {
ivProductImage.setImageResource(product.getImageResId());
tvProductName.setText(product.getName());
tvProductPrice.setText(String.format(Locale.getDefault(), "%.2f 元", product.getPrice()));
tvProductDescription.setText(product.getDescription());
}
btnAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShoppingCart.getInstance().addProduct(product);
Intent intent = new Intent(view.getContext(), ShoppingCartActivity.class);
startActivity(intent);
}
});
}
}
```
在购物车活动中,我们可以使用RecyclerView来显示购物车中的商品列表,并为每个商品添加增加、减少和删除按钮。我们还需要实现一个购物车单例类来管理购物车中的商品列表,并计算总价格。例如:
ShoppingCart.java
```java
public class ShoppingCart {
private static ShoppingCart instance;
private List<Product> productList = new ArrayList<>();
private ShoppingCart() {
}
public static synchronized ShoppingCart getInstance() {
if (instance == null) {
instance = new ShoppingCart();
}
return instance;
}
public void addProduct(Product product) {
productList.add(product);
}
public void removeProduct(Product product) {
productList.remove(product);
}
public void increaseProductQuantity(Product product) {
product.setQuantity(product.getQuantity() + 1);
}
public void decreaseProductQuantity(Product product) {
if (product.getQuantity() > 1) {
product.setQuantity(product.getQuantity() - 1);
} else {
productList.remove(product);
}
}
public double getTotalPrice() {
double totalPrice = 0;
for (Product product : productList) {
totalPrice += product.getPrice() * product.getQuantity();
}
return totalPrice;
}
public List<Product> getProductList() {
return productList;
}
}
```
ShoppingCartAdapter.java
```java
public class ShoppingCartAdapter extends RecyclerView.Adapter<ShoppingCartAdapter.ViewHolder> {
private List<Product> productList;
public ShoppingCartAdapter(List<Product> productList) {
this.productList = productList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shopping_cart, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Product product = productList.get(position);
holder.ivProductImage.setImageResource(product.getImageResId());
holder.tvProductName.setText(product.getName());
holder.tvProductPrice.setText(String.format(Locale.getDefault(), "%.2f 元", product.getPrice()));
holder.tvProductQuantity.setText(String.valueOf(product.getQuantity()));
holder.btnIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShoppingCart.getInstance().increaseProductQuantity(product);
notifyDataSetChanged();
}
});
holder.btnDecrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShoppingCart.getInstance().decreaseProductQuantity(product);
notifyDataSetChanged();
}
});
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShoppingCart.getInstance().removeProduct(product);
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return productList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView ivProductImage;
public TextView tvProductName;
public TextView tvProductPrice;
public TextView tvProductQuantity;
public Button btnIncrease;
public Button btnDecrease;
public Button btnDelete;
public ViewHolder(View itemView) {
super(itemView);
ivProductImage = itemView.findViewById(R.id.iv_product_image);
tvProductName = itemView.findViewById(R.id.tv_product_name);
tvProductPrice = itemView.findViewById(R.id.tv_product_price);
tvProductQuantity = itemView.findViewById(R.id.tv_product_quantity);
btnIncrease = itemView.findViewById(R.id.btn_increase);
btnDecrease = itemView.findViewById(R.id.btn_decrease);
btnDelete = itemView.findViewById(R.id.btn_delete);
}
}
}
```
ShoppingCartActivity.java
```java
public class ShoppingCartActivity extends AppCompatActivity {
private RecyclerView rvShoppingCart;
private TextView tvTotalPrice;
private ShoppingCartAdapter shoppingCartAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart);
rvShoppingCart = findViewById(R.id.rv_shopping_cart);
tvTotalPrice = findViewById(R.id.tv_total_price);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
rvShoppingCart.setLayoutManager(layoutManager);
shoppingCartAdapter = new ShoppingCartAdapter(ShoppingCart.getInstance().getProductList());
rvShoppingCart.setAdapter(shoppingCartAdapter);
tvTotalPrice.setText(String.format(Locale.getDefault(), "总价:%.2f 元", ShoppingCart.getInstance().getTotalPrice()));
}
}
```
请注意,上述代码仅提供了基本的实现,您需要根据自己的需求进行修改和扩展。
阅读全文