Как правильно определить высоту CardView в одном item-е у RecyclerView?
К примеру, вот мой код. Он практически работает, проблема в том, из 7 раз вызова onCreateViewHolder - только 6 раз нормально определяется высота, а первый раз пишет что объект у которого я хочу найти высоту == null. P/S проблема в закоменченом коде...
Код адаптера:
class Adapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var maxColumnHeight: Int = 543
private var maxStepsValue: Long = 0
var list: List<Pair<Int, WorkoutJournalItem>> = mutableListOf()
//==============================================================================================
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.layout_statistics_item, parent, false)
// try {
// val column = parent.findViewById<CardView>(R.id.statistics_item_column)
// val columnHeight = column.measuredHeight
// Log.d("ml", columnHeight.toString())
//
// } catch (t: Throwable){
// Log.d("ml", t.message)
// }
return StepsHolder(v)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
Log.d("ml", holder.itemView.height.toString())
val current = list[position]
(holder as StepsHolder).bind(current)
}
override fun getItemCount(): Int = list.size
fun setData(list: List<Pair<Int, WorkoutJournalItem>>) {
this.list = list
// поиск max-значения шагов
val itemWithMaxValue = list.maxBy { it.second.steps }
maxStepsValue = itemWithMaxValue?.second?.steps?.toLong() ?: 0
notifyDataSetChanged()
}
//----------------------------------------------------------------------------------------------
private inner class StepsHolder(item: View) : RecyclerView.ViewHolder(item) {
private val column = item.findViewById<CardView>(R.id.statistics_item_column)
private val tv_date = item.findViewById<TextView>(R.id.statistics_item_tv)
fun bind(current: Pair<Int, WorkoutJournalItem>) {
tv_date.text = DRY.getDateFromTimestamp(current.first.toString())
}
}
private fun calcColumnHeight(item: WorkoutJournalItem): Int {
return ((maxColumnHeight * item.steps) / maxStepsValue).toInt()
}
}