recycter点击的数据怎么显示在另一recycter

2个recycterview,类似点餐的情形。其中一个recycter添加了4个texiview,有品类和价格2个参数,点击哪个TextView就把参数显示在另一个recycter中。

我在TextAdapter的点击事件中向drinkList添加数据,不过报错行不通。
零基础自学,还有很多不懂的,希望能详细的指点。

MainActivity.kt

package android.example.drink

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity(){
    private val textList = ArrayList()
    public val drinkList = ArrayList()
    public lateinit var driAdapter: DrinkAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerDrink: RecyclerView = findViewById(R.id.recyclerView)
        recyclerDrink.layoutManager = LinearLayoutManager(this)
        driAdapter = DrinkAdapter(drinkList)
        recyclerDrink.adapter = driAdapter

        val recyclerText: RecyclerView = findViewById(R.id.reCycTextView)
        recyclerText.layoutManager = GridLayoutManager(this,3)
        val textTer = TextAdapter(textList)
        recyclerText.adapter = textTer
        initText()
    }

    private fun initText() {
        textList.add(PinLei("可乐", 8))
        textList.add(PinLei("奶茶", 10))
        textList.add(PinLei("冰红茶", 2))
        textList.add(PinLei("+冰块", 3))
    }
}

Drinks.kt

package android.example.drink

class Drinks(var name: String, var price: Int)

Pinlei.kt

package android.example.drink

class PinLei(val pn1: String, val price: Int)

DrinkAdapter.kt

package android.example.drink

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class DrinkAdapter(private val drinkList: List) :
    RecyclerView.Adapter() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val xinXi1: TextView = view.findViewById(R.id.text1)
        val xinXi2: TextView = view.findViewById(R.id.text2)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.drink_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val drink = drinkList[position]
        holder.xinXi1.text = drink.name
        holder.xinXi2.text = drink.price.toString()
    }

    override fun getItemCount() = drinkList.size
}

TextAdapter.kt

package android.example.drink

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class TextAdapter(private val textList: List) :
    RecyclerView.Adapter() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val pinLei: TextView = view.findViewById(R.id.typeBt)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.pinlei_ltem, parent, false)
        val viewHolder = ViewHolder(view)


        viewHolder.pinLei.setOnClickListener {
            val position = viewHolder.bindingAdapterPosition
            val text = textList[position]
            MainActivity().drinkList.add(Drinks(text.pn1, text.price) )
            MainActivity().driAdapter.notifyItemInserted(MainActivity().drinkList.size -1)
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val drink = textList[position]
        holder.pinLei.text = drink.pn1
    }

    override fun getItemCount() = textList.size
}

activity_main.xml


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/reCycTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1">
    androidx.recyclerview.widget.RecyclerView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/reCycTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="152dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">
    androidx.recyclerview.widget.RecyclerView>

androidx.constraintlayout.widget.ConstraintLayout>

drink_item.xml


<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="24sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:textSize="24sp"
        android:textColor="@color/black"/>

androidx.appcompat.widget.LinearLayoutCompat>

pinlei_item.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp">

    <Button
        android:id="@+id/typeBt"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="24sp"
        android:backgroundTint="#686868"
        />

LinearLayout>
运行结果及报错内容

img

不清楚为啥你的driAdapter没有实例化 建议是在适配器中通过接口把数据抛出去处理 而不是在适配器中处理

class TextAdapter(private val textList: List<PinLei> ,val click:OnTextItemClick) :
    RecyclerView.Adapter<TextAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val pinLei: TextView = view.findViewById(R.id.typeBt)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.pinlei_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val drink = textList[position]
        holder.pinLei.text = drink.pn1
        holder.pinLei.setOnClickListener {
            val position = holder.position
            val text = textList[position]
            click.click(pn1 =text.pn1 , price =   text.price)
//            MainActivity().drinkList.add(Drinks(text.pn1, text.price) )
//            MainActivity().driAdapter?.notifyItemInserted(MainActivity().drinkList.size -1)
        }
    }

    override fun getItemCount() = textList.size
    interface  OnTextItemClick{
        fun click(pn1 :String,price:Int)
    }

}
//MainActivity
val recyclerText: RecyclerView = findViewById(R.id.reCycTextView)
        recyclerText.layoutManager = GridLayoutManager(this, 3)
        val textTer = TextAdapter(textList,object :TextAdapter.OnTextItemClick{
            override fun click(pn1: String, price: Int) {
                drinkList.add(Drinks(pn1 ,price))
                driAdapter?.notifyDataSetChanged()
            }
        })

img


没有看明白这里为什么要用 lateinit 啊,非必要的话可以去掉试一下