NestedScrollView动态设置子View高度不生效

java 复制代码
 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (!mFillViewport) {
            return;
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.UNSPECIFIED) {
            return;
        }

        if (getChildCount() > 0) {
            View child = getChildAt(0);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int childSize = child.getMeasuredHeight();
            int parentSpace = getMeasuredHeight()
                    - getPaddingTop()
                    - getPaddingBottom()
                    - lp.topMargin
                    - lp.bottomMargin;

            if (childSize < parentSpace) {
                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                        getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin,
                        lp.width);
                int childHeightMeasureSpec =
                        MeasureSpec.makeMeasureSpec(parentSpace, MeasureSpec.EXACTLY);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }
    }

当child高度小于parent的可用高度时,子view的高度会重置为parentSpace!

如果要修改其子view高度,也要同时修改NestedScrollView的高度。

PS:

java 复制代码
    /**
     * When set to true, the scroll view measure its child to make it fill the currently
     * visible area.
     */
    private boolean mFillViewport;
  1. 看注释,这个参数的意思就是子view填满父view,但这里都return出去了,子view高度是怎么测量的?

  2. 都有 if (childSize < parentSpace){...},这参数有没有没什么区别吧?

相关推荐
张紫娃1 分钟前
idea 常用快捷键
java·ide·intellij-idea
兮动人5 分钟前
Java应用全链路故障排查实战指南:从系统资源到JVM深度诊断
java·开发语言·jvm
风流 少年12 分钟前
Cursor创建Spring Boot项目
java·spring boot·后端
wáng bēn20 分钟前
【java17】使用 Word 模板导出带替换符、动态表格和二维码的文档
java·word·itextpdf
全栈凯哥1 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
M1A12 小时前
Java集合框架深度解析:LinkedList vs ArrayList 的对决
java·后端
Top`2 小时前
Java 泛型 (Generics)
java·开发语言·windows
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ2 小时前
如何使用Java WebSocket API实现客户端和服务器端的通信?
java·开发语言·websocket
是小崔啊2 小时前
tomcat源码02 - 理解Tomcat架构设计
java·tomcat
小袁拒绝摆烂2 小时前
SQL开窗函数
android·sql·性能优化