【发布时间】:2019-01-20 13:07:00
【问题描述】:
我正在尝试让我的应用程序使用 if 语句并检测特定的可绘制对象是否设置为 ImageView。但是,if 部分由于某种原因永远不会被执行(总是else 部分)。当我在 if 语句中使用 constantState 时,我真的不明白为什么这不起作用。
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)!!
}
}
【问题讨论】:
-
我认为使用 constantState 进行映射是这里的问题。为了比较您的图像,请查看此解决方案,简单且有效stackoverflow.com/questions/37575857/…
标签: android kotlin android-recyclerview android-imageview android-drawable