I have an Activity
, called 'HomeActivity', where I have a bottom navigation menu and four fragments. The activity has a FloatingActionButton
and when I hit this, it opens a bottomSheetFragment
which contain a RecyclerView
and when I hit an item from it, I want some values to send to the fragment
('HomeFragment') which is part of the 'HomeActivity'
After doing a lot of searches online, I have done the following but I am not able to get the data in the fragment
.
Following is the fragment
from which I want to send the data.
'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()
}
})
}
}
Following is the adapter of the above fragment ('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)
}
Following is the fragment
where I want to receive the data.
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
}
}
All I know is that I am not doing things right, please help me fix it.
CodePudding user response:
In this line,
fragmentManager?.beginTransaction()?.replace(R.id.nav_host_fragment,HomeFragment())?.commit()
Replace HomeFragment()
with myFragment
because that's where you are attaching the bundle.
See if this solves your problem.