I have RecycleView in Fragment. When I click one of the items in RecyclerView, I want to open new Fragment. The second Fragment did not replace the previous but the Fragment is overwritten
Image. How can I fix this?
My adapter:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Get element from your dataset at this position and replace the
// contents of the view with that element
val currentProduct = productList[position]
holder.image.setImageBitmap(currentProduct.image)
holder.title.text = currentProduct.title
holder.price.text = currentProduct.price.toString() "€"
//Go to product details
holder.itemView.setOnClickListener {
val activity = it.context as AppCompatActivity
val detailsFragment = ProductDetailsFragment()
activity.supportFragmentManager.beginTransaction().replace(R.id.fragment,detailsFragment).addToBackStack(null).commit()
}
}
CodePudding user response:
Use navigation component and smoothly navigateTo(action_from1fragment_to2fragment).
If you want to open specific dialog, fragment with specific data you can use lambda in adapter to provide id to viewmodel, fragment.
CodePudding user response:
Simple ans is to add elevation and background color to parent layout of second fragment and set arguments like clickable and focusable to true.
your onClick for recyclerView is working fine.
