让NestedScrollView成功嵌套ExpandableListView

1,474 阅读1分钟

让NestedScrollView成功嵌套ExpandableListView

最近想让一个ExpandableListView + 一些控件 一起滑动,在网上搜了好久的例子

,最后终于看到一篇文章,明白要重写ExpandableListView

一下是重写代码

public class NonScrollExpandableListView extends ExpandableListView {

    public NonScrollExpandableListView(Context context) {
        super(context);
    }

    public NonScrollExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonScrollExpandableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

之后,就可以开开心心地一起滑动啦

其实也可以通过给ExpandableListView设置Header或Footer等方式来实现,想要的效果,但这不灵活,代码量大,所以放弃了

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