Mashiba Sakuto
Mashiba Sakuto

Reputation: 223

FireBaseRecyclerAdapter how to open Fragment when clicking on an item and show data?

I want to open a new Fragment when an item is clicked from the FireBaseRecyclerAdapter.

PersonAdapter.kt:

override fun onBindViewHolder(
        holder: personsViewholder,
        position: Int, model: Exercise
    ) {


        holder.title.text = model.title

        holder.description.text = model.description
}

At the moment I can fill the recyclerview with data but how can I pass on this data to a new Fragment, for example PersonDetailFragment?

What I tried

 var activity: AppCompatActivity = it.context as AppCompatActivity

                   activity.supportFragmentManager.beginTransaction()
                  .replace(R.id.chat_profile_dimmer, ExercisesFragment(model.title, model.description)).addToBackStack(null).commit()

I already tried several things but nothing seems to work.

Upvotes: 0

Views: 51

Answers (1)

Himanshu Choudhary
Himanshu Choudhary

Reputation: 370

You can do this by using a callback. Follow the steps below:

  1. Make a callback in the adapter constructor.

    class YourAdapter(private val callback: (DataTypeToBeSent) -> Unit): ...

  2. In onBindViewHolder:

    holder.view.setOnClickListener {
    callback.invoke(model)
    }

With this, you will get the data in the view (Activity/Fragment) where the adapter is called, every time the button/view is clicked in the RecyclerView.

Then from that view(Activity/Fragment), you can then start the fragment.

Upvotes: 1

Related Questions