(Kotlin) RecyclerView button change outside ImageView

30 views Asked by At

I have an adapter class that structures a RecyclerView. I want this RecyclerView to have a button that updates an ImageView outside of the RecyclerView. This code updates the images inside the RecyclerView, but I can't get it to update the image outside.

class CustomAdapter(private val mList: List<ItemsViewModel>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {


    // create new views
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        // inflates the card_view_design view
        // that is used to hold list item
        val mainXML = LayoutInflater.from(parent.context)
            .inflate(R.layout.activity_main, parent, false)
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.card_view_design, parent, false)

        return ViewHolder(view, mainXML)
    }

    // binds the list items to a view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val ItemsViewModel = mList[position]

        // sets the image to the imageview from our itemHolder class
        Picasso.get().load("https://img.pokemondb.net/sprites/x-y/normal/" + ItemsViewModel.text + ".png").into(holder.imageView)

        // sets the text to the textview from our itemHolder class
        holder.textView.text = ItemsViewModel.text
        holder.buttonView.setOnClickListener {

                Log.e("GIF selecter", holder.pokemonSelect.toString())
                holder.pokemonSelect.setImageResource(R.drawable.ic_launcher_background)
                Picasso.get().load("https://img.pokemondb.net/sprites/x-y/normal/golbat.png").into(holder.pokemonSelect) //This is the line of troubles, changing it to "holder.imageView" changes the image inside the recyclerview. I want it to change an image outside the recyclerview
            }
        }

    // return the number of the items in the list
    override fun getItemCount(): Int {
        return mList.size
    }

    // Holds the views for adding it to image and text
    class ViewHolder(ItemView: View, mainPage: View) : RecyclerView.ViewHolder(ItemView) {
        val pokemonSelect: ImageView = mainPage.findViewById(R.id.pokemonSelect)
        val imageView: ImageView = itemView.findViewById(R.id.pokemonImg)
        val textView: TextView = itemView.findViewById(R.id.pokemon)
        val buttonView: Button = itemView.findViewById(R.id.changeGIF)
    }
}
1

There are 1 answers

1
Rajan Kali On

The problem is you are inflating activity_main.xml inside createViewHolder which does nothing as it not pointing to the actual activity_main instance instead creates a new instance of View which is not rendered/attached to hierarchy at all. Even if you set third parameter of inflate to true, it would still be child of recycler item not main layout, the ideal way it to pass the required outside ImageView in constructor of adapter

class CustomAdapter(
    private val mList: List<ItemsViewModel>, 
    private val pokemonSelect: ImageView) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
...//in onBind
holder.buttonView.setOnClickListener {
    Log.e("GIF selecter", holder.pokemonSelect.toString())
    holder.pokemonSelect.setImageResource(R.drawable.ic_launcher_background)
    // get rid of holder
    Picasso.get().load("https://img.pokemondb.net/sprites/x-y/normal/golbat.png").into(pokemonSelect) 
 }
...

// In MainActivity

customAdapter = CustomAdapter(list, findViewById(R.id.pokemonSelect))