简体   繁体   中英

How to do modal bottom sheet dialog in recyclerview with kotlin

When I click on an item in recyclerView, I want Modal Bottom Sheet to be opened. But I want the medicine name of the item I clicked in recyclerView to appear in medicine name in the text inside bottom_sheet.xml.

Here is my recyclerViewAdapter:

class RecyclerViewAdapter(val mnArrayList: ArrayList<String>, val nmArrayList: ArrayList<String>): RecyclerView.Adapter<RecyclerViewAdapter.MyHolder>() {


    class MyHolder(val binding: RecyclerRowBinding) : RecyclerView.ViewHolder(binding.root){


    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
       val binding= RecyclerRowBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return MyHolder(binding)
    }


    override fun onBindViewHolder(holder: MyHolder, position: Int) {

        holder.binding.textViewMName.text= mnArrayList.get(position)    //medicine name
        holder.binding.textViewMAdet.text = nmArrayList.get(position)   //medicine quantity

        holder.itemView.setOnClickListener {
            Toast.makeText(holder.itemView.context,"Clicked!",Toast.LENGTH_LONG).show()

            val dialog = BottomSheetDialog(holder.itemView.context)
            val view= LayoutInflater.from(holder.itemView.context).inflate(R.layout.bottom_sheet,null)
            val textSheet= view.findViewById<TextView>(R.id.bottomSheetText)

            textSheet.text= mnArrayList.get(position)

            println(textSheet.toString())

            dialog.show()
        }

    }

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


}

Here is my bottom_sheet.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/bottomSheetText"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="medicine"
        android:textSize="20sp"
        android:textColor="@color/black"
        android:gravity="center">

    </TextView>

</LinearLayout>

How do I do this?

您必须在此处为其指定特定位置的值(定义一个变量):

textSheet.text = mnArrayList.get(position)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM