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)!!
}
}