Search code examples
androidkotlinandroid-recyclerviewandroid-imageviewandroid-drawable

RecyclerView adapter not recognising current drawable in ImageView


I'm trying to get my app to use an if statement and detect if a specific drawable is set to an ImageView. However, the if part never gets executed for some reason (always the else part). I really don't understand why this isn't working when I used constantState in the if statement.

class MyRVAdapter(private val myList: ArrayList<Facility>): RecyclerView.Adapter<MyRVAdapter.ViewHolder>() {
    override fun getItemCount(): Int {
        return myList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mIVExpandCollapse.setOnClickListener {
            if (holder.mIVExpandCollapse.drawable.constantState == ContextCompat.getDrawable(holder.mIVExpandCollapse.context, R.drawable.ic_keyboard_arrow_down)!!.constantState) {
                    Toast.makeText(holder.mIVExpandCollapse.context, "Hi there! This is a Toast.", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(holder.mIVExpandCollapse.context, "Hi there! This is not a Toast.", Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
        return ViewHolder(v)
    }

    class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
        val mIVExpandCollapse = itemView.findViewById<ImageView>(R.id.iv_expandcollapsearrow)!!
    } 
}

Solution

  • Try to maintain a boolean called isExpanded in each item of your list. Check the expansion using it, not views, because in scrolling the list, the views get recycled and your desired state gets lost also. Here is an implementation of this idea:

    Facility.kt

    data class Facility(
    
            /* Other fields are here, */
    
            var isExpanded: Boolean = false
    )
    

    MyRVAdapter.kt

    class MyRVAdapter(private val myList: ArrayList<Facility>) : RecyclerView.Adapter<MyRVAdapter.ViewHolder>() {
        override fun getItemCount(): Int {
            return myList.size
        }
    
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val item = myList[position]
            holder.mIVExpandCollapse.setOnClickListener {
                if (item.isExpanded) {
                    Toast.makeText(holder.mIVExpandCollapse.context, "Hi there! This is an expanded item.", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(holder.mIVExpandCollapse.context, "Hi there! This is an collapsed item.", Toast.LENGTH_LONG).show()
                }
                item.isExpanded = !item.isExpanded
            }
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
            return ViewHolder(v)
        }
    
        class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val mIVExpandCollapse = itemView.findViewById<ImageView>(R.id.iv_expandcollapsearrow)!!
        }
    
    }