Search code examples
androidandroid-fragmentsandroid-recyclerviewandroid-fragmentactivity

How to pass data from RecyclerView present inside FragmentA to FragmentB?


I am trying to pass an object related to an item from a recycler view when user clicks on the item. I can somehow change the fragment but null value is passed to the second Fragment. Here is my Fragment which contains the Recycler View

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val rootView = inflater.inflate(R.layout.fragment_movie_list, container, false)

    rootView.rvMovieList.layoutManager = LinearLayoutManager(activity)
    rootView.rvMovieList.addItemDecoration(DividerItemDecoration(activity, DividerItemDecoration.VERTICAL))

    val moviesList = listOf(
        Movies("Civil War", "Chris Evans", 2016, 7.8),
        Movies("Avengers", "RDJ", 2012, 8.1),
        Movies("Age of Ultron", "Chris Evans", 2015, 7.2),
        Movies("Infinity War", "RDJ", 2018, 8.9),
        Movies("Aquaman", "Jason Momoa", 2018, 8.3)
    )

    val adapter = MovieDetailsAdapter(context, moviesList)
    rootView.rvMovieList.adapter = adapter



    return rootView
}

override fun onAttach(context: Context?) {
    super.onAttach(context)
    if (context is OnItemClickListener)
        listener = context
}

interface OnItemClickListener {
    fun onItemClickListener(instanceList: Movies)
}

companion object {
    val context = this
    lateinit var clickedInstance: Movies
    lateinit var listener: OnItemClickListener
}

Here is the adapter. When I click on the item, the listener must pass the Object related to the clicked item to the second Fragment.

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {


    return MovieViewHolder(
    LayoutInflater.from(parent.context).inflate(
        R.layout.recycler_view_movie_list, parent, false
    )
)
}

override fun getItemCount(): Int {
    return movieNames.size
}

override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
    holder.setData(movieNames[position])
}


inner class MovieViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    lateinit var currName: Movies

    init {
        itemView.setOnClickListener {



        }
    }

    fun setData(movieName: Movies) {
        itemView.tvRVMovieLists.text = movieName.title
        this.currName = movieName
    }
}

Here is the second fragment

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    val rootView = inflater.inflate(R.layout.fragment_movie_details, container, false)


    arguments?.run {
        rootView.tvTitle?.text = getString("MOVIE_TITLE")
        rootView.tvActor?.text = getString("MOVIE_ACTOR")
        rootView.tvYear?.text = getInt("MOVIE_YEAR").toString()
        rootView.tvRating?.text = getDouble("MOVIE_RATING").toString()
    }

    return rootView
}

I tried making the interface listener as companion object. It just opens the second fragment. But the data is not passed.


Solution

  • You can't communicate directly from a fragment to another, you should pass the data from Fragment A to its activity, then pass the data from the activity to Fragment B.