webview height在nestedScrollView中无限增长

onu*_*pus 10 android webview android-coordinatorlayout android-collapsingtoolbarlayout nestedscrollview

这是我的布局.当我加载google.com时,webview的高度会无限增长.webview的onSizeChange函数不断被调用,我可以看到webview不断无限扩展.我已经尝试了22.2.1和23.1.0的设计和appcompat库并没有效果.

有解决方案吗 : - |

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.example.ajay.scrollabletoolbar.MyWebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"/>
</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">

    <android.support.v7.widget.Toolbar
        android:id="@+id/restricted_browser_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true">

            <EditText
                android:id="@+id/current_url"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:backgroundTint="@android:color/darker_gray"
                android:dropDownAnchor="@+id/restricted_browser_toolbar"
                android:hint="hint hint"
                android:imeOptions="actionGo|flagNoExtractUi"
                android:inputType="textNoSuggestions|textUri"
                android:paddingBottom="8dp"
                android:paddingEnd="8dp"
                android:paddingStart="8dp"
                android:singleLine="true"/>

            <ImageView
                android:id="@+id/clear_url_text"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignRight="@+id/current_url"
                android:layout_centerVertical="true"
                android:layout_marginRight="8dp"
                android:src="@android:drawable/ic_menu_close_clear_cancel"
                android:visibility="gone"/>
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

김준호*_*김준호 8

用一句话,你就是不能把你的WebView内心NestedScrollView.这是按预期工作的.

如果你把一个WebView内部NestedScrollView(或ScrollView),你WebView的尺寸与View.MeasureSpec.UNSPECIFIED要求.无论你设置MATCH_PARENT甚至固定大小如100dp WebView的高度.NestedScrollView迫使其子女按View.MeasureSpec.UNSPECIFIED要求衡量自己.这一切都发生在NestedScrollViewmeasureChild()方法.它如下所示:

@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);
}
Run Code Online (Sandbox Code Playgroud)

并且MeasureSpec.UNSPECIFIED意味着,正如其标签所暗示的那样,它可以是它想要的任何尺寸.我认为来自Google I/O 2013"编写Android自定义视图"会话的幻灯片将帮助您理解它.

在此输入图像描述

实际上这是一个在这个帖子中讨论过的问题,根据这位Google工程师的说法,

这就像在桌面中一样,您将浏览器窗口的大小调整为页面的高度,使其无法垂直滚动.滚动发生在NestedScrollView而不是webview本身.因此,页面中没有js的结果会看到任何滚动事件,因此它不知道您已滚动到页面末尾加载更多内容.

所以,底线是,不要把你的WebView内心NestedScrollView.您应该WebView滚动网页本身.快乐的编码!:)