Recyclerview 卡片项目 Onclick Kotlin [英] Recyclerview Card Item Onclick Kotlin
问题描述
我想在用户分别点击 cardview 时创建一个新的意图/活动,我想知道如何在 kotlin 中做到这一点.我注意到大多数教程都是用 Java 编写的,而我是一个没有学过 Java 的学生.
包 com.example.ttshfypj.adapters导入 androidx.fragment.app.Fragment导入 android.view.View导入 android.view.LayoutInflater导入 android.view.ViewGroup导入 androidx.recyclerview.widget.RecyclerView导入 com.example.ttshfypj.data_class.medications导入 android.widget.ImageView导入 android.widget.TextView导入 com.example.ttshfypj.R导入 com.squareup.picasso.Picasso导入 kotlinx.android.synthetic.main.list_medication.view.*类药物适配器(瓦尔药物列表:ArrayList<药物>):RecyclerView.Adapter(){覆盖乐趣 onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {val v = LayoutInflater.from(parent.context).inflate(R.layout.list_medication, parent, false)返回 ViewHolder(v)}覆盖乐趣 getItemCount(): Int {返回药物清单.大小}覆盖乐趣 onBindViewHolder(holder: ViewHolder, position: Int) {val 药物组:药物 = 药物列表[位置]持有人?.medicationnamevar?.text = druggroup.medicineN持有人?.medicationschedulevar?.text = druggroup.MedicineTimeval imagemedicine = holder?.itemView?.medicationimageviewPicasso.get().load(medicationgroup.MedicineImage).into(imagemedicine)}类 ViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {val drugnamevar = itemView.findViewById(R.id.medicationname) as TextViewval drugchedulevar = itemView.findViewById(R.id.medicationschedule) 作为 TextView}}
如果您正在使用 kotlin,您还可以使用方法引用,您可以直接在适配器内部传递要调用的方法,并且可以直接在那里调用它.
Activity.kt
<预><代码>适配器(this::itemClickHandler);私人乐趣 itemClickHandler(int position){//你的逻辑在这里}Adapter.kt
class Adapter(val itemClickHandler:(Int)->Unit):RecyclerView.Adapter(){覆盖乐趣 onBindViewHolder(){//你可以像这样在这里调用它itemClickHandler.invoke(adapterPosition);}}
请参考此完整代码
MainActivity.kt
包 com.example.myapplication导入 androidx.appcompat.app.AppCompatActivity导入 android.os.Bundle导入 android.util.Log导入 androidx.recyclerview.widget.LinearLayoutManager导入 kotlinx.android.synthetic.main.activity_main.*类 MainActivity : AppCompatActivity() {覆盖 fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val list = mutableListOf()for(i 在 1..25){list.add(Items(CHILD,"Child"))}与(rvList){layoutManager = LinearLayoutManager(this@MainActivity)适配器 = 适配器(列表,这个@MainActivity::onItemClickHandler)}}私人乐趣 onItemClickHandler(position:Int){Log.d("***","${position}");//在这里你可以开始一个新的意图来点击项目打开一个新的活动}}
适配器代码如下
包 com.example.myapplication导入 android.view.LayoutInflater导入 android.view.View导入 android.view.ViewGroup导入 androidx.recyclerview.widget.RecyclerView导入 kotlinx.android.synthetic.main.layout_child.view.*导入 kotlinx.android.synthetic.main.layout_header.view.*const val 头 = 1;const val 儿童 = 2;数据类项目(val 类型:Int,val 文本:String)类适配器(私有 val 列表:List, val itemClickHandler: (Int) -> Unit):RecyclerView.Adapter() {覆盖乐趣 onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderViewHolder {val headerView =LayoutInflater.from(parent.context).inflate(R.layout.layout_header, parent, false)val headerViewHolder = HeaderViewHolder(headerView)headerView.setOnClickListener {itemClickHandler.invoke(headerViewHolder.adapterPosition)}返回 headerViewHolder}覆盖乐趣 getItemCount(): Int = list.size覆盖乐趣 onBindViewHolder(holder: HeaderViewHolder, position: Int) {持有人.onBind(列表[位置].文本)}内部类 HeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {有趣的 onBind(文本:字符串){itemView.tvHeader.text = 文本}}}
希望这对你有帮助,你可以关注
I want to create a new intent/activity when the user clicks on a cardview respectively and i was wondering how does one do it in kotlin. i noticed most tutorials are in java and i'm a student who did not learn java.
package com.example.ttshfypj.adapters
import androidx.fragment.app.Fragment
import android.view.View
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.ttshfypj.data_class.medications
import android.widget.ImageView
import android.widget.TextView
import com.example.ttshfypj.R
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.list_medication.view.*
class medicationAdapter (val medicationlist: ArrayList<medications>) : RecyclerView.Adapter<medicationAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_medication, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return medicationlist.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val medicationgroup: medications = medicationlist[position]
holder?.medicationnamevar?.text = medicationgroup.medicineN
holder?.medicationschedulevar?.text = medicationgroup.MedicineTime
val imagemedicine = holder?.itemView?.medicationimageview
Picasso.get().load(medicationgroup.MedicineImage).into(imagemedicine)
}
class ViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {
val medicationnamevar = itemView.findViewById(R.id.medicationname) as TextView
val medicationschedulevar = itemView.findViewById(R.id.medicationschedule) as TextView
}
}
If you are using kotlin you can also use method reference where you directly pass a method to be called inside the adapter and you can directly invoke it there.
Activity.kt
Adapter(this::itemClickHandler);
private fun itemClickHandler(int position){
// your logic here
}
Adapter.kt
class Adapater(val itemClickHandler:(Int)->Unit):RecyclerView.Adapter(){
override fun onBindViewHolder(){
//you can invoke it here like this
itemClickHandler.invoke(adapterPosition);
}
}
please refer this full code
MainActivity.kt
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val list = mutableListOf<Items>()
for(i in 1..25){
list.add(Items(CHILD,"Child"))
}
with(rvList) {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = Adapter(list, this@MainActivity::onItemClickHandler)
}
}
private fun onItemClickHandler(position:Int){
Log.d("***","${position}");
//here you can start a new intent to open a new activity on click of item
}
}
The adapter code is as following
package com.example.myapplication
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.layout_child.view.*
import kotlinx.android.synthetic.main.layout_header.view.*
const val HEADER = 1;
const val CHILD = 2;
data class Items(val type: Int, val text: String)
class Adapter(private val list: List<Items>, val itemClickHandler: (Int) -> Unit) :
RecyclerView.Adapter<Adapter.HeaderViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderViewHolder {
val headerView =
LayoutInflater.from(parent.context).inflate(R.layout.layout_header, parent, false)
val headerViewHolder = HeaderViewHolder(headerView)
headerView.setOnClickListener {
itemClickHandler.invoke(headerViewHolder.adapterPosition)
}
return headerViewHolder
}
override fun getItemCount(): Int = list.size
override fun onBindViewHolder(holder: HeaderViewHolder, position: Int) {
holder.onBind(list[position].text)
}
inner class HeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun onBind(text: String) {
itemView.tvHeader.text = text
}
}
}
Hope this helps and you are able to follow
这篇关于Recyclerview 卡片项目 Onclick Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!