1 year ago
#257861
Mearko
nested recyclerview to open a new activity
I am new to Android developing and I am stuck and can't find solution how can I do onClick()
event to open new activity for nested RecyclerView
this is MainActivity.kt
package com.example.recyclerviewmultitypeexample
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.*
import kotlin.collections.ArrayList
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var dataList = ArrayList<Data>()
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_SEVEN, "","","","","","","","február 28 - marec 06"," ",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_THREE, "Novinka na webstránke: ","Aktualizovaný","školský semafor - po jarných prázdninách","","" ,"","","Pia 04. mar","09:53",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_FOUR, "Bronislava Fačkovcová → Rodičia:","","Vážení rodičia,zasielam Vám Aktualizovaný školský semafor - platný po jarných prázdninách Do domácej izolácie ide iba žiak s pozitívnym...","","","","","","09:16 "," • lajkov: 19"))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_SEVEN, "","","","","","","","február 21 - 27"," ",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_TWO, "DÚ • ","Matematika"," • DÚ na jarné","prázdniny - PZ str 72 až 86","prehrať online"," • do: Piatok 07.03","","Sob 26. feb","09:51",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_FOUR, "Marcela Hanková → Rodičia:","Dobrý","deň, deto dostaváli tento týždeň šeky, poprosila by som vás o uhradenie v priebehu budúceho týždňa. Ak by ste mali nejasnosti, kľudne sa na mňa...","","","","","Štv 24. feb","12:40 "," • lajkov: 13"))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_FOUR, "Bronislava Fačkovcová → Rodičia:","","Vážení rodičia, oznamujeme Vám, že výdaj AG testov pre Vaše deti bude realizovaný v dňoch: 24.2.2022 od 13,30 do 16,30 hod 25.2.2022 od 06,03....","","","","","","12:34 ","lajkov: 27"))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_TWO, "DÚ • ","Dejepis"," • Veľmoci proti Hitlerovi","","prehrať online"," • do: Piatok 25.02","","Str 23. feb","13:26",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_SEVEN, "","","","","","","","február 14 - 20"," ",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_TWO, "DÚ • ","Anglický jazyk"," • Posielam nové ","učivo - verbs and nouns z dnešnej hodiny /18.2./. Opravná písomka z trpného rodu a zo slovíčok bude vo štvrtok 24.2.","prehrať online"," • do: Piatok 25.02","","Pia 18. feb","15:59",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_FOUR, "Bronislava Fačkovcová → 9.B + 10:","Milí","žiaci, oznamujem Vám, že narúšanie on - line hodín a zneužívanie identity iného žiaka je hrubým porušením školského poriadku. Vedenie školy rieši narúšanie...","","","","","","14:08 "," • lajkov: 19"))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_FOUR, "Silvia Habánová → 9.B • Slovenský jazyk a literatúra:","","Milí deviataci, hodina literatúry bude,ale z vážnych dôvodov sa možem pripojiť až okolo 11. Prosím, od 10.50 buďte už pripojení a robte...","","","","","","07:41 "," • lajkov: 1"))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_FOUR, "Gabriela Kašáková → Rodičia 9.B + 6:",""," Dobrý deň, dovoľte mi informovať Vás, že prihlášky na štúdium na SŠ pre všetkých žiakov 9.ročníka su vytlačené a pripravené na podpis zákonnými...","","","","","Štv 17. feb","19:26 "," • lajkov: 4"))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_TWO, "DÚ • ","Matematika"," • Ihlan - materiál na","hodinu"," prehrať online"," • do: Piatok 18.02","","","13:46",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_TWO, "DÚ • ","Matematika"," • Kužeľ - príklady 15 a","16"," prehrať online"," • do: Štvrtok 17.02","","","10:30",""))
dataList.add(Data(RecyclerViewAdapter.VIEW_TYPE_THREE, "Novinka na webstránke: ","Okresné kolo","Dejepisnej olympiády","","" ,"","","Str 16. feb","18:31"," • lajkov: 2"))
val adapter = RecyclerViewAdapter(this, dataList)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter
System.out.println("hovno")
}
}
this is RecyclerViewAdapter.kt
package com.example.recyclerviewmultitypeexample
import android.content.ClipData.Item
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView.OnItemClickListener
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
class RecyclerViewAdapter(context: Context, list: ArrayList<Data>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val VIEW_TYPE_ONE = 1
const val VIEW_TYPE_TWO = 2
const val VIEW_TYPE_THREE = 3
const val VIEW_TYPE_FOUR = 4
const val VIEW_TYPE_FIVE = 5
const val VIEW_TYPE_SIX = 6
const val VIEW_TYPE_SEVEN = 7
}
private val context: Context = context
var list: ArrayList<Data> = list
private inner class View1ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textView)
var messages: TextView = itemView.findViewById(R.id.textView1)
var messagess: TextView = itemView.findViewById(R.id.textView4)
var messagesss: TextView = itemView.findViewById(R.id.textView2)
var messagessss: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textData
messages.text = recyclerViewModel.textDu1
messagess.text = recyclerViewModel.textDu2
messagesss.text = recyclerViewModel.textDu3
messages.text = recyclerViewModel.textCas
messagessss.text = recyclerViewModel.textDatum
}
}
private inner class View2ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textView)
var messages: TextView = itemView.findViewById(R.id.textView1)
var messagess: TextView = itemView.findViewById(R.id.textView2)
var messagesss: TextView = itemView.findViewById(R.id.textView3)
var messagesssss: TextView = itemView.findViewById(R.id.textPrehrat)
var messagessssss: TextView = itemView.findViewById(R.id.textView5)
var messagesssssss: TextView = itemView.findViewById(R.id.textTime)
var messagessss: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textData
messages.text = recyclerViewModel.textDu1
messagess.text = recyclerViewModel.textDu2
messagesss.text = recyclerViewModel.textDu3
messagesssss.text = recyclerViewModel.textDu4
messagessssss.text = recyclerViewModel.textDu5
messagesssssss.text = recyclerViewModel.textCas
messagessss.text = recyclerViewModel.textDatum
}
}
private inner class View3ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textView)
var messages: TextView = itemView.findViewById(R.id.textView1)
var messagess: TextView = itemView.findViewById(R.id.textView2)
var messagesssssss: TextView = itemView.findViewById(R.id.textTime)
var messagessss: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textData
messages.text = recyclerViewModel.textDu1
messagess.text = recyclerViewModel.textDu2
messagesssssss.text = recyclerViewModel.textCas
messagessss.text = recyclerViewModel.textDatum
}
}
private inner class View4ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textView)
var messages: TextView = itemView.findViewById(R.id.textView1)
var messagess: TextView = itemView.findViewById(R.id.textView2)
var messagesssss: TextView = itemView.findViewById(R.id.textLike)
var messagesssssss: TextView = itemView.findViewById(R.id.textTime)
var messagessss: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textData
messages.text = recyclerViewModel.textDu1
messagess.text = recyclerViewModel.textDu2
messagesssss.text = recyclerViewModel.textLike
messagessss.text = recyclerViewModel.textDatum
messagesssssss.text = recyclerViewModel.textCas
}
}
private inner class View5ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textView)
var messages: TextView = itemView.findViewById(R.id.textView2)
var messagess: TextView = itemView.findViewById(R.id.textView1)
var messagesssss: TextView = itemView.findViewById(R.id.textView4)
var messagesssssss: TextView = itemView.findViewById(R.id.textTime)
var messagessss: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textData
messages.text = recyclerViewModel.textDu1
messagess.text = recyclerViewModel.textDu2
messagesssss.text = recyclerViewModel.textDu3
messagesssssss.text = recyclerViewModel.textCas
messagessss.text = recyclerViewModel.textDatum
}
}
private inner class View6ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textView)
var messages: TextView = itemView.findViewById(R.id.textView1)
var messagesssss: TextView = itemView.findViewById(R.id.textView4)
var messagessssss: TextView = itemView.findViewById(R.id.textView5)
var messagesssssss: TextView = itemView.findViewById(R.id.textTime)
var messagessss: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textData
messages.text = recyclerViewModel.textDu1
messagesssss.text = recyclerViewModel.textDu2
messagessssss.text = recyclerViewModel.textDu3
messagesssssss.text = recyclerViewModel.textCas
messagessss.text = recyclerViewModel.textDatum
}
}
private inner class View7ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var message: TextView = itemView.findViewById(R.id.textDatum)
fun bind(position: Int) {
val recyclerViewModel = list[position]
message.text = recyclerViewModel.textDatum
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if (viewType == VIEW_TYPE_ONE) {
return View1ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_chybanie, parent, false)
)
}
else if(viewType == VIEW_TYPE_TWO){
return View2ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_du, parent, false)
)
}
else if(viewType == VIEW_TYPE_THREE){
return View3ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_news, parent, false)
)
}
else if(viewType == VIEW_TYPE_FOUR){
return View4ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_sprava, parent, false)
)
}
else if(viewType == VIEW_TYPE_FIVE){
return View5ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_suplovanie, parent, false)
)
}
else if(viewType == VIEW_TYPE_SEVEN){
return View7ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_datum, parent, false)
)
}
return View6ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_znamka, parent, false)
)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (list[position].viewType === VIEW_TYPE_ONE) {
(holder as View1ViewHolder).bind(position)
}
else if(list[position].viewType === VIEW_TYPE_TWO){
(holder as View2ViewHolder).bind(position)
}
else if(list[position].viewType === VIEW_TYPE_THREE){
(holder as View3ViewHolder).bind(position)
}
else if(list[position].viewType === VIEW_TYPE_FOUR){
(holder as View4ViewHolder).bind(position)
}
else if(list[position].viewType === VIEW_TYPE_FIVE){
(holder as View5ViewHolder).bind(position)
}
else if(list[position].viewType === VIEW_TYPE_SEVEN){
(holder as View7ViewHolder).bind(position)
}
else {
(holder as View6ViewHolder).bind(position)
}
}
override fun getItemViewType(position: Int): Int {
return list[position].viewType
}
}
this is Data.kt
package com.example.recyclerviewmultitypeexample
data class Data(val viewType: Int, val textData: String,val textDu1 : String,val textDu2 : String,val textDu3 : String,val textDu4 : String,val textDu5 : String,val textDu6 : String,val textDatum : String,val textCas : String,val textLike : String)
android
android-studio
kotlin
android-recyclerview
nestedrecyclerview
0 Answers
Your Answer