Обнаружение щелчка для recyclerview внутри recyclerview

Я реализовал recyclerview в recyclerview. Вот мой код

activity_main

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/rv_activity_main"
            app:layoutManager="android.support.v7.widget.GridLayoutManager"
            app:spanCount="2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

MainActivity

class MainActivity : AppCompatActivity() {

    private var graduationList = ArrayList<Graduation>()
    private var yearList = ArrayList<YearList>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initList()
        rv_activity_main.adapter = MainAdapter(graduationList)
    }

    private fun initList(){
        graduationList.clear()
        yearList.clear()

        yearList.add(YearList("2011"))
        yearList.add(YearList("2012"))
        yearList.add(YearList("2013"))
        yearList.add(YearList("2014"))
        yearList.add(YearList("2015"))
        yearList.add(YearList("2016"))
        yearList.add(YearList("2017"))
        yearList.add(YearList("2018"))
        yearList.add(YearList("2019"))

        graduationList.add(Graduation("UG",yearList))
        graduationList.add(Graduation("PG",yearList))
    }
}

Graduation Pojo Class

data class Graduation(
    var name: String,
    var yearList: ArrayList<YearList>
)

YearList Pojo Class

data class YearList(var yearName: String)

MainAdapter

class MainAdapter(private var graduationList: ArrayList<Graduation>): RecyclerView.Adapter<MainAdapter.MainViewHolder>(){

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MainViewHolder {
        val itemView = LayoutInflater.from(p0.context).inflate(R.layout.cell_main_adapter,p0,false)
        return MainViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return graduationList.size
    }

    override fun onBindViewHolder(p0: MainViewHolder, p1: Int) {
        p0.graduationTextView.text = graduationList[p1].name
        p0.graduationRecyclerView.adapter = SecondAdapter(graduationList[p1].yearList)

    }

    inner class MainViewHolder(view: View): RecyclerView.ViewHolder(view){
        var graduationTextView: TextView = view.findViewById(R.id.tv_cell_main_adapter)
        var graduationRecyclerView: RecyclerView = view.findViewById(R.id.rv_cell_main_adapter)
    }
}

SecondAdapter

class SecondAdapter(private var yearList: ArrayList<YearList>): RecyclerView.Adapter<SecondAdapter.SecondViewHolder>(){

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): SecondViewHolder {
        val itemView = LayoutInflater.from(p0.context).inflate(R.layout.cell_second_adapter,p0,false)
        return SecondViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return yearList.size
    }

    override fun onBindViewHolder(p0: SecondViewHolder, p1: Int) {
        p0.yearTextView.text = yearList[p1].yearName

        p0.itemView.setOnClickListener {
            Log.i("Pritish",yearList[p1].yearName+"\n")
        }
    }

    inner class SecondViewHolder(view: View): RecyclerView.ViewHolder(view){
        var yearTextView: TextView = view.findViewById(R.id.tv_cell_year_title)
    }
}

ItemClick для MainAdapter не должен быть интерактивным, поэтому я не добавил для него никаких itemClick. Предположим, пользователь нажимает конкретный год. Я знаю, какой год выбрал пользователь, но у меня вопрос, как узнать, когда был выбран год, из UG или PG, без реализации прослушивателя itemclick для MainAdapter?

0
0
30
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Попробуйте так,

Передайте объект Graduation в конструкторе SecondAdapter из MainAdapter, как показано ниже.

p0.graduationRecyclerView.adapter = SecondAdapter(graduationList[p1])

Второй адаптер

class SecondAdapter(private var graduation: Graduation): RecyclerView.Adapter<SecondAdapter.SecondViewHolder>(){

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): SecondViewHolder {
        val itemView = LayoutInflater.from(p0.context).inflate(R.layout.cell_second_adapter,p0,false)
        return SecondViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return graduation.yearList.size
    }

    override fun onBindViewHolder(p0: SecondViewHolder, p1: Int) {
        p0.yearTextView.text = graduation.yearList[p1].yearName

        p0.itemView.setOnClickListener {
            Log.i("Pritish",graduation.yearList[p1].yearName+" "+graduation.name+"\n")
        }
    }

    inner class SecondViewHolder(view: View): RecyclerView.ViewHolder(view){
        var yearTextView: TextView = view.findViewById(R.id.tv_cell_year_title)
    }
}

Большое спасибо. Такой простой ответ

Pritish 31.10.2018 14:49

Другие вопросы по теме