ListViewを全てのアイテムを表示できる高さで配置したい場合があります。そんなときのためのカスタムViewです。
class NonScrollListView : ListView {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init {
isVerticalScrollBarEnabled = false
isHorizontalScrollBarEnabled = false
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val customHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE shr 2, View.MeasureSpec.AT_MOST)
super.onMeasure(widthMeasureSpec, customHeightMeasureSpec)
layoutParams.height = measuredHeight
}
}
あとは普通にレイアウトxmlの中で配置して、通常のListView通りにアダプタを設定してやればOKです。