Issue
I want to add onClick on my RecyclerView item images for navigating to Browser but it requires context how can I access the context
class MovieAdapter(val movies: List<Data>, val activity: Activity) : RecyclerView.Adapter<MovieAdapter.ViewHolder>(){
class ViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
val txtTitle = itemView.findViewById<TextView>(R.id.txtName)
val txtYear = itemView.findViewById<TextView>(R.id.txtPrice)
val image = itemView.findViewById<ImageView>(R.id.image)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_movie,parent,false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return movies.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val movie:Data=movies[position]
holder.txtTitle.setText(movie.animeName)
holder.txtYear.setText(movie.animeİd.toString())
Glide.with(activity).load(movie.animeİmg).into(holder.image)
holder.image.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW,Uri.parse("https://www.google.com/search?q="+holder.txtTitle))
startActivity(intent)
}
}
}
Solution
You can add a listener to your adapter. You can listen to this listener from your adapter. I will put a code snippet below to give you an idea.
MovieAdapter(val movies: List<Data>, val fooListener: (Sting) -> Unit)
//When you create the adapter instance(Activity)
MovieAdapter(list){ url ->
val intent = Intent(Intent.ACTION_VIEW,Uri.parse(url))
startActivity(intent)
}
Answered By - Mustafa Kuloğlu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.