我有一个用Kotlin编写的购物车应用程序的RecyclerViewAdapter。当用户更改购物车中的物品数量时,我想用当前购物车总数更新ActionBar的标题。
我已经看过使用回调,侦听器和传递上下文的各种代码,以尝试从RecyclerViewAdapter访问ActionBar,但是我不知道它们是否适用于Kotlin,或者总是将所有内容都保留下来也没有太大意义。在RecyclerViewAdapter中的代码(数据库查找,写入,CardView更新等)
以下是我的调用代码和RecyclerViewAdapter的高度缩写代码(但我希望我拥有所有重要的组件):
class ItemRecyclerViewAdapter(private val itemArray: MutableList<ItemObject>, private val contextABC: Context) : RecyclerView.Adapter<ItemRecyclerViewAdapter.ViewHolder>() {
inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
private val cardViewAdd: Button
init {
cardViewAdd = v.findViewById<View>(R.id.cardview_item_Add) as Button
// Add one to Qty Ordered
cardViewAdd.setOnClickListener { updateItemQty(adapterPosition, itemArray[adapterPosition].qtyOrdered + 1, v) }
}
}
private fun updateItemQty(arrayPosition: Int, pQtyOrdered: Int, v: View) {
itemArray[arrayPosition].qtyOrdered = pQtyOrdered
notifyItemChanged(arrayPosition)
}
}
class CartActivity : BaseActivity() {
internal var cartItemsList: MutableList<ItemObject> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.cart_master)
cartItemsList = DatabaseAdapter.cartItems
cart_RecyclerView.setHasFixedSize(true)
cart_RecyclerView.layoutManager = LinearLayoutManager(this)
rvAdapter = ItemRecyclerViewAdapter(cartItemsList, this)
cart_RecyclerView.adapter = rvAdapter
}
}
有人可以让我朝正确的方向前进吗?我当然感谢您的帮助!
[编辑] 在Birju Vachhani的大力帮助下,这是我的工作代码:
class ItemRecyclerViewAdapter(private val itemArray: MutableList<ItemObject>,
private val contextABC: Context, val updateActionBarTitle: (ItemObject) -> Unit = {})
: RecyclerView.Adapter<ItemRecyclerViewAdapter.ViewHolder>() {
inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
private val cardViewAdd: Button
init {
cardViewAdd = v.findViewById<View>(R.id.cardview_item_Add) as Button
// Add one to Qty Ordered
cardViewAdd.setOnClickListener { updateItemQty(adapterPosition, itemArray[adapterPosition].qtyOrdered + 1, v) }
}
}
private fun updateItemQty(arrayPosition: Int, pQtyOrdered: Int, v: View) {
itemArray[arrayPosition].qtyOrdered = pQtyOrdered
notifyItemChanged(arrayPosition)
updateActionBarTitle(itemArray[arrayPosition])
}
}
class CartActivity : BaseActivity() {
internal var cartItemsList: MutableList<ItemObject> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.cart_master)
cartItemsList = DatabaseAdapter.cartItems
cart_RecyclerView.setHasFixedSize(true)
cart_RecyclerView.layoutManager = LinearLayoutManager(this)
rvAdapter = ItemRecyclerViewAdapter(cartItemsList, this){item->
supportActionBar?.setTitle("hi")
}
cart_RecyclerView.adapter = rvAdapter
}
}
答案 0 :(得分:0)
您可以通过使用Kotlin的lambda函数来做到这一点:
适配器:
class ItemRecyclerViewAdapter(
private val itemArray: MutableList<ItemObject>,
val onItemAdded: (ItemObject) -> Unit = {}
) :
RecyclerView.Adapter<ItemRecyclerViewAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return itemArray.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.cardViewAdd.setOnClickListener {
updateItemQty(position, itemArray[position].qtyOrdered + 1)
onItemAdded(itemArray[position])
}
}
class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val cardViewAdd: Button = v.findViewById(R.id.cardview_item_Add)
fun bind(item:ItemObject){
// bind your data with view here
}
}
fun updateItemQty(arrayPosition: Int, pQtyOrdered: Int) {
itemArray[arrayPosition].qtyOrdered = pQtyOrdered
notifyItemChanged(arrayPosition)
}
}
在您的活动中:
CartActivity类:BaseActivity(){ 内部var cartItemsList:MutableList = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.cart_master)
cartItemsList = DatabaseAdapter.cartItems
cart_RecyclerView.setHasFixedSize(true)
cart_RecyclerView.layoutManager = LinearLayoutManager(this)
rvAdapter = ItemRecyclerViewAdapter(cartItemsList, this){item->
// here you can access your ActionBar as perform actions on it
}
cart_RecyclerView.adapter = rvAdapter
}
}