kotlin android recyclerview button click viewbinding code example
Example 1: button inside a recycle view in android in kotlin
fun intialize(item: PacketModel, action: onPacketItemClickListener) {
var date = (((item.date) as Timestamp).seconds) * 1000L
fun convertLongToTime(time: Long): String {
val date = Date(time)
val format = SimpleDateFormat("yyyy.MM.dd HH:mm")
format.timeZone = TimeZone.getTimeZone("GMT+5")
return format.format(date)
}
var convertedDate = convertLongToTime(date)
packetTime.text = convertedDate
timeMessage.text = ""
drugs.text = item.drugs
note.text = item.notes
dosage.text = item.dosage
itemView.setOnClickListener {
action.onCardClick(item, adapterPosition)
}
myButton.setOnClickListener {
// Do whatever you want on your button click as like you did to your recycler-view item click
//action.onCardClick(item, adapterPosition)
}
}
Example 2: recyclerview onclicklistener kotlin
val data = list[position]holder.itemView.setOnClickListener { cellClickListener.onCellClickListener(data)}
Example 3: recyclerview onclicklistener kotlin
class MainActivity : AppCompatActivity(), CellClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val recyclerView: RecyclerView = findViewById(R.id.recycler_view) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = Adapter(this, fetchList(), this) } private fun fetchList(): ArrayList { val list = arrayListOf() for (i in 0..9) { val model = Model(R.drawable.ic_collections_black_24dp, "Title : $i", "Subtitle : $i") list.add(model) } return list } override fun onCellClickListener() { Toast.makeText(this,"Cell clicked", Toast.LENGTH_SHORT).show() }}