ListAdapter:获取片段中的item或itemPos

时间:2019-02-06 11:33:51

标签: android kotlin listadapter

我有以下具有RecyclerViewListAdapter个用户的片段:

....
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片段(而不是祝酒词)。

1 个答案:

答案 0 :(得分:1)

您必须将回调更改为类似

val adapter = UsersAdapter { user ->
    Toast.makeText(context, "Clicked $user", Toast.LENGTH_SHORT).show()
}