如何使用 kotlin android 制作我的 Firebase Recycler 适配器 onClick 的项目
how to make the items of my Firebase Recycler adapter onClick with kotlin android
在 FirestoreUI 的帮助下,我建立了一个 recyclerview,其中包含来自我的云 firestore 数据库的项目。现在我希望能够在单击此列表的项目后触发事件*(*该事件基本上是一个新的 activity,我可以在其中找到有关该项目的更多详细信息)我正在使用 Kotlin 和 android工作室。
这是我放置所有需要的代码的 activity :
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
}
在 FirestoreUI 的帮助下,我建立了一个 recyclerview,其中包含来自我的云 firestore 数据库的项目。现在我希望能够在单击此列表的项目后触发事件*(*该事件基本上是一个新的 activity,我可以在其中找到有关该项目的更多详细信息)我正在使用 Kotlin 和 android工作室。 这是我放置所有需要的代码的 activity :
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
}