我正在开发一个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?
答案 0 :(得分:1)
为什么不简单地通过Bottom sheet的构造函数参数传递数据
val comentariosBottomSheet = ComentariosBottomSheet(data)//open bottom sheet
comentariosBottomSheet.show(fragmentManager, comentariosBottomSheet.tag)
答案 1 :(得分:0)
他的方法如何:
Note函数作为最后一个参数传递
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)
}
如上所述,该函数作为最后一个参数传递给Aconstructor,因此我们可以像这样调用它:
val myAdapter:MyAdapter = MyAdapter(){
//ShowBottomScree
// 'it' is refenced as the passed data from adapter.
}