Home > database >  setOnlickListener recycleview (in a fragment) navigate to another fragment
setOnlickListener recycleview (in a fragment) navigate to another fragment

Time:12-04

I have recycleview in fragment. When i click one of the items in recyclerview,ı 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.

  • Related