0

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)
sam_1234
  • 117
  • 12
  • 1
    [This is what you need](https://stackoverflow.com/questions/49969278/recyclerview-item-click-listener-the-right-way) Creating interface in recyclerview , implementing it in your activity so what ever click is made inside your recyclerview you get that in your activity and do rest. – Malik Saifullah Sep 13 '22 at 17:51
  • ahh thank you very much! i just figured this out through rigorous googling lol. but now my problem is that its scrolling to the correct position but not the expanded view. When one of the movie card items is clicked, the move description view expands but you can't see it because the screen has not scrolled it into view. I'll link a short video of the issue here soon. – sam_1234 Sep 13 '22 at 18:05

0 Answers0