【问题标题】:Getting views inside BottomSheetDialogFragment from a RecyclerView adapter从 RecyclerView 适配器获取 BottomSheetDialogFragment 中的视图
【发布时间】:2019-01-22 21:57:05
【问题描述】:

我有一个RecyclerView。这个RecyclerView 视图的每个单元格都有一个按钮,可以从Adapter 中调用BottomSheetDialogFragment

当从我的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() 中访问我的删除按钮

【问题讨论】:

  • 希望this answer 可以解决您的问题以获取BottomSheet 的基本观点

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


【解决方案1】:

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
        }

    }
}

【讨论】:

  • bottomSheet.view?不幸返回 null
  • 使用 executePendingTransactions 调用编辑了答案。希望应该工作。 @PhilBlais
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 2018-06-06
  • 2016-10-03
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
相关资源
最近更新 更多