0

我有一个Activity名为“HomeActivity”的,其中有一个底部导航菜单和四个片段。活动有一个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
    }
}

我只知道我做的不对,请帮我解决它。

4

1 回答 1

1

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

替换HomeFragment()为,myFragment因为那是您附加捆绑包的地方。

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

于 2021-11-09T07:54:33.570 回答