Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将RecyclerView数据发送到BottomSheetDialog_Android_Kotlin_Android Recyclerview_Bottom Sheet - Fatal编程技术网

Android 如何将RecyclerView数据发送到BottomSheetDialog

Android 如何将RecyclerView数据发送到BottomSheetDialog,android,kotlin,android-recyclerview,bottom-sheet,Android,Kotlin,Android Recyclerview,Bottom Sheet,我正在开发一个Recyclerview,按下按钮应该会打开一个BottomSheet对话框。我可以打开底部工作表,但无法将数据传递给它。我之前试过使用一个界面,但是没有用 class MyAdapter(private val listaItens: List<Itens>, private val context: Context, private val fragmentManager: FragmentManager) :

我正在开发一个Recyclerview,按下按钮应该会打开一个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 ...
            }
        }
    }
class MyAdapter(私有val列表:列表,私有val上下文:上下文,
专用val碎片管理器:碎片管理器):
RecyclerView.Adapter(){
私有val POST_TXT=0
私人val POST\u IMG=1
//一些代码。。。
override fun onCreateViewHolder(父级:ViewGroup,viewType:Int):RecyclerView.ViewHolder{
何时(视图类型){
POST_TXT->{
val item=LayoutInflater.from(parent.context)。充气(R.layout.text,parent,false)
返回ViewHolderExtra(项目)
}
POST_IMG->{
val item=LayoutInflater.from(parent.context)。充气(R.layout.image,parent,false)
返回ViewHolderImage(项目)
}
//一些代码。。。
}
}
覆盖onBindViewHolder(holder:RecyclerView.ViewHolder,位置:Int){
val项目=列表项[位置]
val usuarioLogado=UsuarioFirebase.getdadosuariologado()
何时(holder.itemViewType){
POST_TXT->{
val viewHolderTexto=作为viewHolderTexto的支架
viewHolderTexto.setIsRecyclable(错误)
//一些代码。。。
}
POST_IMG->{
val viewHolderImage=保持架为viewHolderImage
viewHolderImage.setIsRecyclable(错误)
//一些代码。。。
holder.ImageComEntarioPostageImage.setOnClickListener{
val comentariosBottomSheet=comentariosBottomSheet()//打开底部工作表
显示(碎片管理器,comentariosBottomSheet.tag)
}
//一些代码。。。
}
}
}

如何将RecyclerView数据发送到BottomSheetDialog?

他的方法如何:

  • 传递到适配器的函数/接口
注意:函数作为最后一个参数传递

    class MyAdapter( .... private val onClickCallBack:(item :Itens) ... {
     ....
    }
在支架上注册clicklistener

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){
    val item = listaItens[position]
    val usuarioLogado = UsuarioFirebase.getDadosUsuarioLogado()
       ....
     holder.itemView.setOnClickListener{
       onClickCallBack(item)
    }
  • 向要显示底部的适配器添加回调(sheet ex活动)
如上所述,函数作为最后一个参数传递给Aconstructor,因此我们可以像这样调用它:

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

    }

为什么不直接通过底部工作表的构造函数参数传递数据呢

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

将它们通过接口发送到保存片段/活动,并将数据发送到将要打开的片段。是否希望共享所有的RealReVIEW数据,或者仅选择所选的项目?考虑在OntRead创建中设置OnCutlook回调,在上面的示例中设置绑定时间。