webview height grows indefinitely inside a nestedScrollView

后端 未结 2 1519
谎友^
谎友^ 2020-12-09 20:32

This is my layout. When i load google.com, the webview\'s height keeps growing indefinitely. The onSizeChange function of the webview keeps getting called and i can see the

相关标签:
2条回答
  • 2020-12-09 20:55

    I apologize for not closing this question earlier. We cant put a webview inside a nestedScrollView. But the nature of a NSV, it expands its child to its full height so that it an get it working as expected with toolbars to get collapsible/scrollable toolbar etc. Hence if webview is added, the webview will grow either indefinitely or to a large value and hence it will not work as expected. this can be a ref: code.google.com/p/android/issues/detail?id=198965

    0 讨论(0)
  • 2020-12-09 20:59

    In one sentence, you just can't put your WebView inside NestedScrollView. This is working as intended.

    If you put a WebView inside NestedScrollView (or ScrollView), your WebView measures its dimensions with View.MeasureSpec.UNSPECIFIED requirement. No matter you set MATCH_PARENT or even fixed size like 100dp for your WebView's height. NestedScrollView forces its children measure themselves with View.MeasureSpec.UNSPECIFIED requirement. It all happens at NestedScrollView's measureChild() method. It goes like below :

    @Override
    protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
        ViewGroup.LayoutParams lp = child.getLayoutParams();
        int childWidthMeasureSpec;
        int childHeightMeasureSpec;
        childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft()
                + getPaddingRight(), lp.width);
        childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
    

    And MeasureSpec.UNSPECIFIED means, as its label implies, it can be whatever size it wants. I think this slide from the "Writing Custom Views for Android" session at Google I/O 2013 will help you to understand it.

    Actually this is an issue which was discussed in this thread, and according to this Google engineer,

    This is as if in desktop, you resize the browser window to height of the page so it can't scroll vertically. The scrolling is happening in NestedScrollView rather than webview itself. So a consequence of no js in the page sees any scroll events, so it doesn't know you've scrolled to the end of the page load more content.

    So, bottom line is, DO NOT put your WebView inside NestedScrollView. You should let WebView scroll the web page itself. Happy coding! :)

    0 讨论(0)
提交回复
热议问题