I'm trying to scroll to a recycler view item if the item is expanded and/or clicked. I know that binding.recyclerview.layoutManager.scrollToPosition() can achieve this but I can't call the recycler view in the adapter class. Can I pass a boolean and position then say its true when the card item is clicked, then pass that to the main activity somehow..? I'm still very much a beginner so steps to do achieve this with best practices would really help me out. :)
EDIT* So we got the above issue figured out. Now I am having a new issue where the item is scrolled to when clicked, but the expanded description text that appears after clicking is not scrolled into view. Lol ugh. Would really appreciate any help with this.
Hope you are having a great day!
class MainAdapter: RecyclerView.Adapter<MainViewHolder>() {
var movies = mutableListOf<Model>()
@SuppressLint("NotifyDataSetChanged")
fun setMovieList(movies: List<Model>) {
this.movies = movies.toMutableList()
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = AdapterMovieBinding.inflate(inflater, parent, false)
return MainViewHolder(binding)
}
@SuppressLint("NotifyDataSetChanged")
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
val movie = movies[position]
holder.binding.name.text = movie.name
holder.binding.category.text = movie.category
holder.binding.description.text = movie.desc
Glide.with(holder.itemView.context).load(movie.imageUrl).into(holder.binding.imageview)
holder.binding.expandedView.visibility = if (movie.expand) View.VISIBLE else View.GONE
holder.binding.cardLayout.setOnClickListener {
movie.expand = !movie.expand
// scroll to expanded item
notifyDataSetChanged()
}
}
override fun getItemCount(): Int {
return movies.size
}
}
class MainViewHolder(val binding: AdapterMovieBinding) :
RecyclerView.ViewHolder(binding.root)