我有以下具有RecyclerView
和ListAdapter
个用户的片段:
....
private fun setupList(users: List<User>) {
recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
val adapter = UsersAdapter {
Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show()
}
recyclerView.adapter = adapter
adapter.submitList(users)
}
....
这是UsersAdapter
的相关部分:
....
class FlightItemViewHolder(itemView: View, _context: Context) : RecyclerView.ViewHolder(itemView) {
val context = _context
fun bind(item: User, clickListener: (User) -> Unit) {
itemView.name_value.text = item.fullName
itemView.country_value.text = item.country
itemView.seats_value.text = item.subscriptionSeats.toString()
itemView.setOnClickListener { clickListener(item) }
}
}
....
祝酒词显示得很好,但是我想保留一下clicked元素或其位置,以便调用并显示一个Detail
片段(而不是祝酒词)。
答案 0 :(得分:1)
您必须将回调更改为类似
val adapter = UsersAdapter { user ->
Toast.makeText(context, "Clicked $user", Toast.LENGTH_SHORT).show()
}