如何使我的Firebase Recycler适配器上的项目点击kotlin android



在FirestoreUI的帮助下,我建立了一个回收视图,其中包含来自我的云firestore数据库的项目。现在,我希望能够在点击该列表中的一个项目后触发一个事件(*该事件基本上是一个新活动,我可以在其中找到有关该项目的更多详细信息(,我正在使用Kotlin和android studio。这是我放置所有需要的代码的活动:

class UserViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)
class MeetingActivity : AppCompatActivity() {
val db = Firebase.firestore
private val newMeetingActivityRequestCode = 1
private lateinit var rvMeetings : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_meeting)
rvMeetings = findViewById(R.id.rvMeetings)
val query : CollectionReference = db.collection("Meetings")
val options = FirestoreRecyclerOptions.Builder<Meeting>().setQuery(query , Meeting::class.java)
.setLifecycleOwner(this).build()
val adapter = object: FirestoreRecyclerAdapter<Meeting, UserViewHolder>(options){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.meeting_raw_layout, parent, false)
return UserViewHolder(view)
}
override fun onBindViewHolder(holder: UserViewHolder, position: Int, model: Meeting) {
val tvName : TextView = holder.itemView.findViewById(R.id.tv1)
val tvEmojis : TextView = holder.itemView.findViewById(R.id.tv2)
tvName.text = model.name
tvEmojis.text = model.time
}
}
rvMeetings.adapter = adapter
rvMeetings.layoutManager = LinearLayoutManager(this)
val fab: View = findViewById(R.id.fab)
fab.setOnClickListener {
fabOnClick()
}
}
private fun fabOnClick() {
val intent = Intent(this, AddMeetingActivity::class.java)
startActivityForResult(intent, newMeetingActivityRequestCode)
}
}

任何帮助都将不胜感激!

在您的列表项中设置OnClickListener

override fun onBindViewHolder(holder: UserViewHolder, position: Int, model: Meeting) {
val tvName : TextView = holder.itemView.findViewById(R.id.tv1)
val tvEmojis : TextView = holder.itemView.findViewById(R.id.tv2)
tvName.text = model.name
tvEmojis.text = model.time
holder.itemView.setOnClickListener {
//do something
}

相关内容

  • 没有找到相关文章

最新更新