LoginSignup
2
3

More than 3 years have passed since last update.

ScrollView配下のExpandableListViewが展開できない

Posted at

概要

折りたたみ式のViewを作りたいときにたまーに使うExpandableListViewですが、ScrollView配下にあるとうまく展開されず

parentとchildそれぞれのadapterにデータが正常にセットされているのに・・・

原因はListViewを継承しているので、これ自身がスクロールすることが可能
そのため、スクロールできるものにスクロールできるものが入るのが落とし穴というわけでした・・

ScrollView in ListViewとかでググるとまさにこの問題が多く出てくるのですが、今回はExpandableListView自体にスクロールさせる必要は皆無であったため、スクロール自体をなくす方向で修正

実装

class NonScrollExpandableListView : ExpandableListView {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    )

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val expandSpec =
            MeasureSpec.makeMeasureSpec(View.MEASURED_SIZE_MASK, MeasureSpec.AT_MOST)
        super.onMeasure(widthMeasureSpec, expandSpec)
        val params = layoutParams
        params.height = measuredHeight
    }
}

子Viewにわたすサイズ自体を明確に指定してあげるということですね

これをXMLにて入れればうまく展開できました

参考

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3