在AndroidStudio工具中,使用TabLayout和ViewPager2实现购物页面。其中导航标签有3个,分别是推荐页面、食品页面、百货页面,推荐页显示商品列表,每个商品包括图片、商品名称、介绍、价格,点击商品可跳转到该商品详情页。要求一行显示两个商品,一个页面显示四个商品。并实现商品详情页,商品详情页要求: 1. 点击某一个商品进入本商品的详情页,详情页数据包括商品图片、商品名称、商品价格、该商品的详细介绍,商品详情框架只定义一次即可。 2. 商品详情页底部包含一个“购买”按钮,点击购买后,跳转到购物车页面。 3. 购物车页面可实现商品数量的增减、商品的删除以及总价格计算功能。
时间: 2024-02-12 08:06:39 浏览: 115
下面是一个简单的实现购物页面的示例:
1. 在布局文件中添加TabLayout和ViewPager2:
```
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
2. 在Activity中设置ViewPager2的Adapter,并将Adapter和TabLayout关联起来:
```
val adapter = ShoppingPagerAdapter(this)
view_pager.adapter = adapter
TabLayoutMediator(tab_layout, view_pager) { tab, position ->
when (position) {
0 -> tab.text = "推荐"
1 -> tab.text = "食品"
2 -> tab.text = "百货"
}
}.attach()
```
3. 在PagerAdapter中实现每个页面的布局和逻辑:
```
class ShoppingPagerAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {
override fun getItemCount() = 3
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> RecommendFragment()
1 -> FoodFragment()
2 -> GoodsFragment()
else -> throw IllegalArgumentException("Invalid position")
}
}
}
```
4. 在每个Fragment中实现商品列表的布局和逻辑:
```
class RecommendFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_recommend, container, false)
val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.adapter = RecommendAdapter(requireContext(), listOf(
Product(R.drawable.product1, "商品1", "介绍1", 10.0),
Product(R.drawable.product2, "商品2", "介绍2", 20.0),
Product(R.drawable.product3, "商品3", "介绍3", 30.0),
Product(R.drawable.product4, "商品4", "介绍4", 40.0)
))
recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
return view
}
}
class RecommendAdapter(private val context: Context, private val productList: List<Product>) : RecyclerView.Adapter<RecommendAdapter.ProductViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false)
return ProductViewHolder(view)
}
override fun getItemCount() = productList.size
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val product = productList[position]
holder.imageView.setImageResource(product.imageResId)
holder.nameView.text = product.name
holder.descView.text = product.description
holder.priceView.text = "¥${product.price}"
holder.itemView.setOnClickListener {
val intent = Intent(context, ProductDetailActivity::class.java)
intent.putExtra("product", product)
context.startActivity(intent)
}
}
class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.image_view)
val nameView: TextView = itemView.findViewById(R.id.name_view)
val descView: TextView = itemView.findViewById(R.id.desc_view)
val priceView: TextView = itemView.findViewById(R.id.price_view)
}
}
```
5. 实现商品详情页的布局和逻辑:
```
class ProductDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_product_detail)
val product = intent.getParcelableExtra<Product>("product")!!
findViewById<ImageView>(R.id.image_view).setImageResource(product.imageResId)
findViewById<TextView>(R.id.name_view).text = product.name
findViewById<TextView>(R.id.price_view).text = "¥${product.price}"
findViewById<TextView>(R.id.desc_view).text = product.description
findViewById<Button>(R.id.buy_button).setOnClickListener {
val intent = Intent(this, ShoppingCartActivity::class.java)
startActivity(intent)
}
}
}
```
6. 实现购物车页面的布局和逻辑:
```
class ShoppingCartActivity : AppCompatActivity() {
private val productList = mutableListOf<Product>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_shopping_cart)
findViewById<Button>(R.id.add_button).setOnClickListener {
productList.add(Product(R.drawable.product1, "商品1", "介绍1", 10.0))
updateList()
}
findViewById<Button>(R.id.delete_button).setOnClickListener {
productList.removeAt(productList.lastIndex)
updateList()
}
updateList()
}
private fun updateList() {
findViewById<TextView>(R.id.total_price_view).text = "¥${productList.sumByDouble { it.price }}"
findViewById<RecyclerView>(R.id.recycler_view).adapter = ShoppingCartAdapter(this, productList)
}
}
class ShoppingCartAdapter(private val context: Context, private val productList: List<Product>) : RecyclerView.Adapter<ShoppingCartAdapter.ProductViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_shopping_cart, parent, false)
return ProductViewHolder(view)
}
override fun getItemCount() = productList.size
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val product = productList[position]
holder.imageView.setImageResource(product.imageResId)
holder.nameView.text = product.name
holder.priceView.text = "¥${product.price}"
holder.itemView.setOnClickListener {
productList.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, productList.size - position)
(context as ShoppingCartActivity).updateList()
}
}
class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.findViewById(R.id.image_view)
val nameView: TextView = itemView.findViewById(R.id.name_view)
val priceView: TextView = itemView.findViewById(R.id.price_view)
}
}
```
以上是一个简单的实现购物页面、商品详情页和购物车页面的示例。实际项目中可能需要更复杂的逻辑和界面设计,但基本思路是相似的。
阅读全文