首页 > 解决方案 > 从 RecyclerView 适配器获取 BottomSheetDialogFragment 中的视图

问题描述

我有一个RecyclerView. 此视图的每个单元格RecyclerView都有一个按钮,可以BottomSheetDialogFragmentAdapter.

从我的电话中调用时,底部的表格确实会显示并正确关闭Adapter

我希望能够通过点击我的BottomSheetDialogFragment.

这是我BottomSheetDialogFragment课堂上的按钮

    <Button
        android:id="@+id/deleteBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Delete" />

这是我onBindViewHolder()找不到从那里访问 deleteBtn 的方法,BottomSheetDialogFragment因此我可以继续删除我的单元格

override fun onBindViewHolder(holder: SavesAdapterHolder, position: Int) {
    val bottomSheet: BottomSheetDialogFragment = mBottomSheet()
    bottomSheet.setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialogTheme)
    val myActivity = (context as FragmentActivity).supportFragmentManager
    holder.openButton.setOnClickListener {
        bottomSheet.show(myActivity, "bottomSheet $position")

    }
}

有没有人能向我指出如何从我的内部访问我的删除按钮的正确方向?onBindViewHolder()

标签: javaandroidkotlinandroid-recyclerviewbottom-sheet

解决方案


BottomSheet 视图应该可以通过getView()方法访问

override fun onBindViewHolder(holder: SavesAdapterHolder, position: Int) {
    val bottomSheet: BottomSheetDialogFragment = mBottomSheet()
    bottomSheet.setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialogTheme)
    val myActivity = (context as FragmentActivity).supportFragmentManager
    holder.openButton.setOnClickListener {
        bottomSheet.show(myActivity, "bottomSheet $position")

       // execute the commited transaction before trying to access the view
        myActivity.executePendingTransactions()

      // accessing button view
        bottomSheet.view?.findViewById("*your_btn_id*").setOnClickListener{
             //you can remove item here and notify data set
        }

    }
}

推荐阅读