如何将 RecyclerView 数据发送到 BottomSheetDialog

How to send RecyclerView data to a BottomSheetDialog

我正在开发一个 Recyclerview,按下一个按钮应该打开一个 BottomSheetDialog。我可以打开 BottomSheet,但无法向其传递数据。我之前尝试过使用接口,但没有用。

        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 ...
            }
        }
    }

如何将 RecyclerView 数据发送到 BottomSheetDialog?

他的方法怎么样:

  • function/interface 传入适配器

注意函数作为最后一个参数传递

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

在holder上注册clicklistener

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){
    val item = listaItens[position]
    val usuarioLogado = UsuarioFirebase.getDadosUsuarioLogado()
       ....
     holder.itemView.setOnClickListener{
       onClickCallBack(item)
    }
  • 向要显示底部的适配器添加回调(sheet ex Activity)

如上所述,该函数作为最后一个参数传递给构造函数,因此我们可以像这样调用它:

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

    }

你为什么不简单地通过底部的构造函数参数传递数据sheet

val comentariosBottomSheet = ComentariosBottomSheet(data)//open bottom sheet
comentariosBottomSheet.show(fragmentManager, comentariosBottomSheet.tag)