用Kotlin写一个项目第一个Fragment包含一个RecyclerView,每个item的界面为左侧一个水果图 像,右侧显示水果名称和单价,点击任一item可显示第二个Fragment,如 图1a; •第二个Fragment界面如图1b所示,点击其中的“+”,“-“键可更改订 单数量;
时间: 2023-05-26 12:04:15 浏览: 78
•订单数量可以为0-100之间的任意整数,金额根据数量动态计算,保留两位小数;
•购买数量发生变化时,第一个Fragment中对应的水果item右侧单价也需要更新。
1a
1b
以下是Kotlin代码实现:
第一个Fragment:
```
class FruitListFragment : Fragment() {
private lateinit var binding: FragmentFruitListBinding
private lateinit var fruitList: List<Fruit>
private lateinit var adapter: FruitListAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentFruitListBinding.inflate(inflater, container, false)
initData()
initView()
return binding.root
}
private fun initData() {
fruitList = listOf(
Fruit("苹果", 5.8, R.drawable.apple),
Fruit("香蕉", 2.5, R.drawable.banana),
Fruit("橙子", 3.2, R.drawable.orange),
Fruit("草莓", 8.7, R.drawable.strawberry)
)
}
private fun initView() {
adapter = FruitListAdapter(fruitList) { position -> toOrderFragment(position) }
binding.recyclerView.adapter = adapter
binding.recyclerView.layoutManager = LinearLayoutManager(context)
}
private fun toOrderFragment(position: Int) {
val fruit = fruitList[position]
parentFragmentManager.beginTransaction().apply {
replace(R.id.fragment_container, OrderFragment.newInstance(fruit))
addToBackStack(null)
commit()
}
}
}
```
第一个Fragment中RecyclerView的适配器:
```
class FruitListAdapter(
private val fruitList: List<Fruit>,
private val onItemClickListener: (Int) -> Unit
) : RecyclerView.Adapter<FruitListAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemFruitBinding.inflate(inflater, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val fruit = fruitList[position]
holder.binding.ivFruit.setImageResource(fruit.icon)
holder.binding.tvFruitName.text = fruit.name
holder.binding.tvPrice.text = String.format("%.2f 元/kg", fruit.price)
holder.itemView.setOnClickListener { onItemClickListener(position) }
}
override fun getItemCount(): Int {
return fruitList.size
}
inner class ViewHolder(val binding: ItemFruitBinding) : RecyclerView.ViewHolder(binding.root)
}
```
第二个Fragment:
```
class OrderFragment : Fragment() {
private lateinit var binding: FragmentOrderBinding
private lateinit var fruit: Fruit
private var quantity = 0
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentOrderBinding.inflate(inflater, container, false)
initArguments()
initViews()
return binding.root
}
private fun initArguments() {
fruit = requireArguments().getParcelable(ARG_FRUIT)!!
}
private fun initViews() {
binding.ivFruit.setImageResource(fruit.icon)
binding.tvFruitName.text = fruit.name
binding.tvPrice.text = String.format("%.2f 元/kg", fruit.price)
binding.tvQuantity.text = quantity.toString()
binding.btnMinus.setOnClickListener {
if (quantity > 0) {
quantity--
updateOrder()
}
}
binding.btnAdd.setOnClickListener {
if (quantity < 100) {
quantity++
updateOrder()
}
}
}
private fun updateOrder() {
binding.tvQuantity.text = quantity.toString()
val totalPrice = fruit.price * quantity
binding.tvTotalPrice.text = String.format("%.2f 元", totalPrice)
(requireActivity() as MainActivity).updateCartItem(fruit, quantity)
}
companion object {
private const val ARG_FRUIT = "fruit"
fun newInstance(fruit: Fruit): OrderFragment {
val args = Bundle().apply {
putParcelable(ARG_FRUIT, fruit)
}
return OrderFragment().apply {
arguments = args
}
}
}
}
```
第二个Fragment中显示订单信息的布局:
```
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center">
<TextView
android:id="@+id/tv_quantity_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购买数量:" />
<Button
android:id="@+id/btn_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="@+id/tv_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="0"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="0 元"
android:textSize="16sp" />
</LinearLayout>
```
MainActivity中的updateCartItem方法用来更新购物车列表:
```
fun updateCartItem(fruit: Fruit, quantity: Int) {
val index = cartItems.indexOfFirst { it.fruit == fruit }
if (index == -1) {
if (quantity > 0) {
cartItems.add(CartItem(fruit, quantity))
}
} else {
if (quantity > 0) {
cartItems[index].quantity = quantity
} else {
cartItems.removeAt(index)
}
}
adapter.notifyDataSetChanged()
}
```
购物车列表的RecyclerView的适配器:
```
class CartListAdapter(
private val cartItemList: List<CartItem>
) : RecyclerView.Adapter<CartListAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemCartItemBinding.inflate(inflater, parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val cartItem = cartItemList[position]
holder.binding.ivFruit.setImageResource(cartItem.fruit.icon)
holder.binding.tvFruitName.text = cartItem.fruit.name
holder.binding.tvPrice.text = String.format("%.2f 元/kg", cartItem.fruit.price)
holder.binding.tvQuantity.text = cartItem.quantity.toString()
holder.binding.tvTotalPrice.text = String.format(
"%.2f 元", cartItem.fruit.price * cartItem.quantity
)
}
override fun getItemCount(): Int {
return cartItemList.size
}
inner class ViewHolder(val binding: ItemCartItemBinding) : RecyclerView.ViewHolder(binding.root)
}
```
阅读全文