我一直在嘗試將資料(用戶的電子郵件和電話)從我的配接器傳遞到我的片段。根據我在網上閱讀的內容,我應該為此使用一個界面,但我不能使用 bundle 。任何人都可以逐步解釋我應該如何通過捆綁傳遞資料以及如何在片段中接收它。下面是我的配接器。
class ProductAdapter(
private val onItemClick: (item: ProductResultData) -> Unit,
private val items: List<ProductResultData>
): RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
lateinit var context: Context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
context = parent.context
val binding: ProductItemDataBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.rv_product _item,
parent,
false)
return ProductViewHolder(binding)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val productItem = items[position]
holder.bindItem(productItem,context)
}
override fun getItemCount(): Int {
return items.size
}
inner class ProductViewHolder(private val binding: ProductItemDataBinding): RecyclerView.ViewHolder(binding.root){
@SuppressLint("SetTextI18n")
fun bindItem(item: ProductResultData, context: Context){
Glide.with(context)
.load(R.drawable.img_kitchen)
.placeholder(R.drawable.garden_kit)
.into(binding.ivGardenKit)
binding.tvProductName.text = item.product_name
binding.tvPrice.text = "?" " " item.price.toString() "/ Kit"
binding.tvProductDetails.text = item.about_product
binding.cvProducts.setOnClickListener {
onItemClick.invoke(item)
}
}
}
}
uj5u.com熱心網友回復:
看來您的配接器代碼沒問題。首先,只要有 lambda 引數,就必須將其設定為串列的最后一個。
class ProductAdapter(
private val items: List<ProductResultData
private val onItemClick: (ProductResultData) -> Unit
)
// In your activity/fragment
binding.recyclerView.adapter = ProductAdapter(list) { item ->
Bundle().apply {
putParcelable("PRODUCT_ITEM", item)
}.also {
val yourFrag = YourFragment()
yourFrag.args = it
replaceFragment(yourFrag)
}
}
并確保您的ProductResultData實作Parcelable介面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471136.html
