在基础碎片上检测底部对话框片段的关闭

3

我有一个包含列表的recycleviewfragment。在fragment中,我调用另一个底部弹出框dialog fragment,我想知道当我关闭这个底部弹出框dialog fragment时,如何刷新基础fragment中的列表。

我已经尝试了在基础fragment中使用onPauseonResume方法。请帮我解决这个问题。

3个回答

3

当对话框片段被关闭时,将触发此方法。要在对话框片段中覆盖此方法。

@Override
public void onDismiss(@NonNull DialogInterface dialog) {
    super.onDismiss(dialog);
    // use interface to callback method in base fragment
}

你能给我一些例子吗? - Thushara
https://androidwave.com/fragment-communication-using-interface/ - Arul

0

我不确定,但是我尝试了下面的代码,它有效。

 @Override
public void dismiss() {
    super.dismiss();
    ((MyActivity)context).myRefreshFunction();// 
}

0
调用BottomSheetDialogFragment 在点击事件中打开buttomSheetDialgFragment
class GuestFragment : Fragment(){
        .
        .
        .
        .

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            binding.tvInfo.setOnClickListener {
                val dialogFragment = BottomSheetDogGiveReviewFragment(requireContext(),data.ratingData,data)
                // call back method
                dialogFragment.setOnBottomSheetDismissListener  {
                    resumeFragment()
                }
                dialogFragment.show(parentFragmentManager, "My  Fragment")
            }


        }

    }

在BottomSheetDialogFragment中
class BottomSheetDogGiveReviewFragment(context: Context) : BottomSheetDialogFragment() {
        //this is binding xml file
        private var _binding : FragmentBottomSheetDogGiveReviewBinding?=null
        private val binding get() = _binding!!
        
        //make this listener through call over Fragment mehod
        private var onDismissListener: (() -> Unit)? = null
        fun setOnBottomSheetDismissListener(listener: () -> Unit) {
            onDismissListener = listener
        }
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            // Inflate the layout for this fragment
            _binding = FragmentBottomSheetDogGiveReviewBinding.inflate(inflater, container, false)
            val view = binding.root
            return view
        }
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)


            //button click
            binding.tvSubmit.setOnClickListener {
                ....
                ..
                dialog!!.dismiss()
                onDismissListener?.invoke()
            }
        }

        //dialog them
        override fun getTheme(): Int {
            return  R.style.CustomBottomSheetDialog
        }
    }

// 底部弹出对话框主题 // 在您的themes.xml文件中添加主题
//buttom sheet dialog style
        <style name="CustomBottomSheetDialog" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
            <item name="bottomSheetStyle">@style/CustomBottomSheet</item>
        </style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接