-1

I am developing a Recyclerview that pressing a button should open a BottomSheetDialog. I can open BottomSheet but I can't pass data to it. I tried using an interface earlier but it didn't work.

        class MyAdapter(private val listaItens: List<Itens>, private val context: Context,
                private val fragmentManager: FragmentManager) :
        RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val POST_TXT = 0
    private val POST_IMG = 1
    //some code ...

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            POST_TXT -> {
                val item = LayoutInflater.from(parent.context).inflate(R.layout.text, parent, false)
                return ViewHolderTexto(item)
            }
            POST_IMG -> {
                val item = LayoutInflater.from(parent.context).inflate(R.layout.image, parent, false)
                return ViewHolderImage(item)
            }
            //some code ...
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        val item = listaItens[position]
        val usuarioLogado = UsuarioFirebase.getDadosUsuarioLogado()

        when (holder.itemViewType) {
            POST_TXT -> {
                val viewHolderTexto = holder as ViewHolderTexto
                viewHolderTexto.setIsRecyclable(false)
                //some code ...

            }
            POST_IMG -> {
                val viewHolderImage = holder as ViewHolderImage
                viewHolderImage.setIsRecyclable(false)
                //some code ...
                holder.imageComentarioPostagemImage.setOnClickListener {
                    val comentariosBottomSheet = ComentariosBottomSheet()//open bottom sheet
                    comentariosBottomSheet.show(fragmentManager, comentariosBottomSheet.tag)
                }

//some code ...
            }
        }
    }

How to send RecyclerView data to a BottomSheetDialog?

Vitor Ferreira
  • 1,075
  • 1
  • 14
  • 28

2 Answers2

1

Why don't you simply pass the data by constructor param of the Bottom sheet

val comentariosBottomSheet = ComentariosBottomSheet(data)//open bottom sheet
comentariosBottomSheet.show(fragmentManager, comentariosBottomSheet.tag)
Sivaraj
  • 171
  • 9
0

How about his approach:

  • function/interface passed into the adapter

Note function is passed as last parameter

    class MyAdapter( .... private val onClickCallBack:(item :Itens) ... {
     ....
    }

Register clicklistener on the holder

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){
    val item = listaItens[position]
    val usuarioLogado = UsuarioFirebase.getDadosUsuarioLogado()
       ....
     holder.itemView.setOnClickListener{
       onClickCallBack(item)
    }
  • Add callback to the adapter where you want to show the bottom (sheet ex Activity)

As noted above the function was passed as last parameter to the Aconstructor so we can call it after like this:

     val myAdapter:MyAdapter = MyAdapter(){
           //ShowBottomScree 
           //  'it' is refenced as the passed data from adapter. 

    }
Sz-Nika Janos
  • 811
  • 6
  • 19