Android - NestedScrollView which contains ExpandableListView doesn't scroll when expanded

前端 未结 5 580
情书的邮戳
情书的邮戳 2020-11-29 08:40

I have an ExpandableListView inside a NestedScrollView (yes I know, it is not good to have a scrolling view inside another scrolling view but I don

5条回答
  •  庸人自扰
    2020-11-29 09:01

    The answer from V-rund Puro-hit is what worked for me. But it took some modifications to work with Kotlin supporting API >19. So for the purpose of saving someone time, here it is:

    Create a new class file NonScrollExpandableListView:

    import android.content.Context
    import android.util.AttributeSet
    import android.widget.ExpandableListAdapter
    import android.widget.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 setAdapter(adapter: ExpandableListAdapter?) {
            super.setAdapter(adapter)
        }
    
        override fun setOnChildClickListener(onChildClickListener: OnChildClickListener) {
            super.setOnChildClickListener(onChildClickListener)
        }
    
        override fun expandGroup(groupPos: Int) : Boolean {
            return super.expandGroup(groupPos)
        }
    
        override fun expandGroup(groupPos: Int, animate: Boolean) : Boolean {
            return super.expandGroup(groupPos, animate)
        }
    
        override fun isGroupExpanded(groupPosition: Int): Boolean {
            return super.isGroupExpanded(groupPosition)
        }
    
        public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
            val heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
            super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom)
            val params = layoutParams
            params.height = measuredHeight
        }
    }
    

    ...and I use it like so:

        
    
            
    
                
    
                

    As a result, I can comfortably add buttons and other elements to NavigationView side drawer) and it all works nicely in one common ScrollView. Main usage here is when you need to combine multiple ListViews and ExpandableListViews inside the one common ScrollView which would take care of scrolling.

提交回复
热议问题