如何将数据从 BottomSheetFragment 传递到另一个片段?

How to pass data from a BottomSheetFragment to another fragment?

我有一个名为 'HomeActivity' 的 Activity,其中有一个底部导航菜单和四个片段。 activity 有一个 FloatingActionButton,当我点击它时,它会打开一个 bottomSheetFragment,其中包含一个 RecyclerView,当我从中点击一个项目时,我想要发送一些值fragment ('HomeFragment') 是 'HomeActivity'

的一部分

在网上进行了大量搜索后,我做了以下操作,但无法获取 fragment 中的数据。

以下是我要从中发送数据的 fragment

'HomeBottomSheetFragment.kt'

        class HomeBottomSheetFragment: BottomSheetDialogFragment() {
    
        private lateinit var binding: FragmentHomeBottomSheetBinding
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            FirestoreClass().getCategoryList(this)
    
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
        ): View {
            binding = FragmentHomeBottomSheetBinding.inflate(inflater, container, false)
            return binding.root
        }
    
    fun successCategoryList(categoryList: ArrayList<ProdCategories>) {

        binding.rvHomeCategories.visibility = View.VISIBLE
        binding.rvHomeCategories.layoutManager = GridLayoutManager(context, 4)
        binding.rvHomeCategories.setHasFixedSize(true)
        val categoryAdapter = CategoryListAdapter(requireContext(), categoryList)
        binding.rvHomeCategories.adapter = categoryAdapter

        categoryAdapter.setOnClickListener(object :CategoryListAdapter.OnClickListener{
            override fun onClick(position: Int, category: ProdCategories) {

                val myFragment = HomeFragment()
                val bundle = Bundle()
                bundle.putString("category", category.category_name)
                myFragment.arguments = bundle    
                fragmentManager?.beginTransaction()?.replace(R.id.nav_host_fragment,HomeFragment())?.commit()
            }
        })

    }

}

以下是上述片段的适配器('HomeBottomSheetFragment.kt'),'CategoryListAdapter.kt'.

open class CategoryListAdapter(private val context: Context, private var list: ArrayList<ProdCategories>)
    : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var onClickListener: OnClickListener? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHolder(
            HomeBottomsheetCategoryListLayoutBinding.inflate(
                LayoutInflater.from(
                    parent.context
                ), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val model = list[position]

        if (holder is MyViewHolder) {

            GlideLoader(context).loadProductPicture(model.category_image, holder.binding.ivCategoryImage)
            holder.binding.tvCategoryName.text=model.category_name
        }

        holder.itemView.setOnClickListener {
            if (onClickListener != null) {
                onClickListener!!.onClick(position, model)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun setOnClickListener(onClickListener: OnClickListener) {
        this.onClickListener = onClickListener
    }

    interface OnClickListener{
        fun onClick(position: Int, category: ProdCategories)
    }

    private class MyViewHolder(val binding: HomeBottomsheetCategoryListLayoutBinding) : RecyclerView.ViewHolder(binding.root)

}

下面是fragment我要接收数据的地方

class HomeFragment : BaseFragment() {

private var filterCategory: String?= null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHomeBinding.inflate(inflater, container, false)

        val bundle = this.arguments
        if (bundle!=null) {
            filterCategory = bundle.getString("category")
        }

        return binding.root
    }
}

我只知道我做事不对,请帮我改正。

在这一行中, fragmentManager?.beginTransaction()?.replace(R.id.nav_host_fragment,HomeFragment())?.commit()

HomeFragment() 替换为 myFragment,因为这是您附加捆绑包的位置。

看看这是否能解决您的问题。