Home > Back-end >  How to open a fragment with button click in recycler view
How to open a fragment with button click in recycler view

Time:01-18

class Crypto : Fragment(R.layout.fragment_crypto) { private lateinit var recyclerView: RecyclerView private lateinit var cryptolist: ArrayList<crypro_data> private lateinit var cryptoAdapter: cryptoAdapter override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) recyclerView = view.findViewById(R.id.recyclerview) recyclerView.setHasFixedSize(true) recyclerView.layoutManager = LinearLayoutManager(activity)

    cryptolist = ArrayList()

    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"dolar"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lari"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lira"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"sterlingi"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"dolar"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lari"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lira"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"sterlingi"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"dolar"))
    cryptolist.add(crypro_data(R.drawable.ic_baseline_history_24,"lari"))

    cryptoAdapter =  cryptoAdapter(cryptolist)
    recyclerView.adapter = cryptoAdapter

}

}

this is my code on a fragment where i have recyclerview

i would like to know if it would be possible to open a same fragment when clicked on any of the items in the recyclerview.

but each item has to transfer its unique id to opened fragment in order to find out which button was clicked.

CodePudding user response:

To open a Fragment with a button click in a RecyclerView, you can use the setOnClickListener method on the button in your RecyclerView's adapter together with a click listener in the Fragment from where you create the adapter.

Here is an example of how you can do this in an adapter:

class ImageAdapter(private var imageModel: List<ImageModel>) : RecyclerView.Adapter<ImageAdapter.PostViewHolder>() {
...
//The click listener
var onButtonClick: ((ImageModel) -> Unit)? = null

override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
    ...
    //Set the click listener on the button you have in your RV element
    holder.itemView.button.setOnClickListener {
        //Clicked
        onButtonClick?.invoke(imageModel[position])
    }
}
}

In your Fragment, you can implement the listener and set it to the adapter and handle the transition:

lass YourFragment : Fragment() {
...

private val imageAdapter = ImageAdapter(listOf())
...

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    imageAdapter.onButtonClick = { imageModel ->
        // Handle the button click here
        // open a new fragment
        val fragment = AnotherFragment()
        val fragmentManager = childFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()

        // Pass object as extra
        bundle.putParcelable("key", imageModel)
        fragment.arguments = bundle

        fragmentTransaction.replace(R.id.fragment_container, fragment)
        fragmentTransaction.addToBackStack(null)
        fragmentTransaction.commit()
    }
  }
}

And in the AnotherFragment, you can retrieve the object from the arguments property in the onCreate method:

class AnotherFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // Retrieve object from extra
    val myObject = arguments?.getParcelable<MyObject>("key")
 }
}

In this example, the class ImageModel,should implement the Parcelable interface.

Happy Coding!

  • Related