我有一个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()
内部访问删除按钮的正确方向
答案 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
}
}
}