I have a list of movies (RecyclerView
) and when a movie is clicked it goes to another fragment where the movie clicked is displayed. I want to send the data of the movie object through the navigation from the adapter to the fragment using Parcelize
but i'm getting stuck on this.
My Movie data class:
@Parcelize
data class Movie(
@SerializedName("poster_path") val poster_path : String,
@SerializedName("adult") val adult : Boolean,
@SerializedName("overview") val overview : String,
@SerializedName("release_date") val release_date : String,
@SerializedName("genre_ids") val genre_ids : List<Int>,
@SerializedName("id") val id : Int,
@SerializedName("original_title") val original_title : String,
@SerializedName("original_language") val original_language : String,
@SerializedName("title") val title : String,
@SerializedName("backdrop_path") val backdrop_path : String,
@SerializedName("popularity") val popularity : Double,
@SerializedName("vote_count") val vote_count : Int,
@SerializedName("video") val video : Boolean,
@SerializedName("vote_average") val vote_average : Double
) : Parcelable
My nav_graph:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/nav_graph"
app:startDestination="@id/MoviesFragment">
<fragment
android:id="@ id/MoviesFragment"
android:name="com.example.level6rg.ui.MoviesFragment"
android:label="Movies"
tools:layout="@layout/fragment_movies">
<action
android:id="@ id/action_MoviesFragment_to_SingleMovieFragment"
app:destination="@id/SingleMovieFragment">
</action>
</fragment>
<fragment
android:id="@ id/SingleMovieFragment"
android:name="com.example.level6rg.ui.SingleMovieFragment"
android:label="Movie"
tools:layout="@layout/fragment_single_movie">
<action
android:id="@ id/action_SingleMovieFragment_to_MoviesFragment"
app:destination="@id/MoviesFragment" />
<argument
android:name="Movie"
app:argType="com.example.level6rg.model.Movie" />
</fragment>
</navigation>
My MovieAdapter:
class MovieAdapter(private val movies: List<Movie>): RecyclerView.Adapter<MovieAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
private val imageBaseUrl = "https://image.tmdb.org/t/p/original/"
private val binding = ItemMovieBinding.bind(itemView)
fun databind(movie: Movie) {
Glide.with(itemView).load(imageBaseUrl movie.poster_path).into(binding.ivMovie);
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_movie, parent, false)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val movie = movies[position]
holder.databind(movies[position])
holder.itemView.setOnClickListener {
// Navigate to SingleMovieFragment and parse movie object...
}
}
override fun getItemCount(): Int {
return movies.size
}
// class OnClickListener(val clickListener: (movie: Movie) -> Unit) {
// fun onClick(movie: Movie) = clickListener(movie)
// }
}
I've read about safe args
as well but I got stuck on the implementation
CodePudding user response:
When creating a safe arg, you need to choose Custom Parcalable, give your parcelable class and retrieve it in destination.
Take a look here: https://www.youtube.com/watch?v=i3nRZEbkWks&ab_channel=CodinginFlow