从RecyclerView适配器在BottomSheetDialogFragment内部获取视图

时间:2019-01-22 21:57:05

标签: java android kotlin android-recyclerview bottom-sheet

我有一个RecyclerView。此RecyclerView视图的每个单元格都有一个按钮,该按钮从BottomSheetDialogFragment内发出一个Adapter

从我的Adapter调用它时,底页确实显示并正确关闭了

我希望能够通过点击BottomSheetDialogFragment内的按钮来删除单元格。

这是我的BottomSheetDialogFragment类中的按钮

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

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

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()内部访问删除按钮的正确方向

1 个答案:

答案 0 :(得分:0)

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
        }

    }
}