Webview导致无限可滚动的空白区域

时间:2018-01-31 12:37:17

标签: android webview android-nestedscrollview

我有一个在NestedScrollview中包含Webview的布局,

<?xml version="1.0" encoding="utf-8"?>
<com.app.swipetofinish.SwipeDownLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/postContainer"
android:descendantFocusability="blocksDescendants"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/postDetails"
        android:background="?attr/backgroundColor">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"
                android:id="@+id/postFeaturedImage"/>

            <com.app.customizeviews.Lato_Bold_TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textColor="?attr/primaryTextColor"
                android:letterSpacing="0.02"
                android:lineSpacingExtra="6sp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="16dp"
                android:id="@+id/postTitle"
                android:layout_below="@+id/postFeaturedImage"/>

            <com.app.customizeviews.Lato_Regular_TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/postDate"
                android:textColor="?attr/secondaryTextColor"
                android:textSize="14sp"
                android:layout_below="@+id/postTitle"
                android:layout_marginTop="8dp"
                android:layout_marginStart="16dp" />

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/postDate"
                android:layout_marginTop="12dp"/>

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="50dp"
                android:id="@+id/progressBar"
                android:layout_centerHorizontal="true"/>

        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        app:layout_anchorGravity="bottom"
        app:layout_anchor="@id/postDetails"
        android:elevation="25dp"
        android:background="@drawable/bottom_post_card">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_ext_link_enabled"
            android:id="@+id/sourceLink"
            android:layout_marginStart="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_share_dark_article"
            android:id="@+id/postShare"
            app:layout_constraintStart_toEndOf="@id/sourceLink"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/postLike"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_like_article_unselected"
            android:id="@+id/postLike"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toStartOf="@id/postBookmark"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/postShare"/>

        <TextSwitcher
            android:id="@+id/likesCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/postBookmark"
            android:clipToPadding="false"
            android:inAnimation="@anim/slide_up"
            android:outAnimation="@anim/fade_out"
            android:layout_marginEnd="5dp"
            app:layout_constraintStart_toEndOf="@id/postLike"
            app:layout_constraintBottom_toBottomOf="@id/postLike"
            app:layout_constraintTop_toTopOf="@id/postLike" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_bookmark_article_unselected"
            android:layout_centerVertical="true"
            android:layout_marginEnd="20dp"
            android:layout_alignParentEnd="true"
            android:id="@+id/postBookmark"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

这完美地工作,但从最后一天开始,在网络内容结束后开始造成无限空白,就像这样

enter image description here

所有白色空白区域都是由Webview的主要内容之后的Webview引起的。这种情况只有在我使用ViewPager在片段中打开此布局时才会发生,但如果布局在具有单个页面活动的布局中打开则不会发生。 而这一直到昨天工作到2-3个月,但是从昨天起我就遇到了这个问题。

在Webview中加载内容的代码:

mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setLoadsImagesAutomatically(true);
        mWebView.setVerticalScrollBarEnabled(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebView.setNestedScrollingEnabled(false);
        } else {
            mParentView.setNestedScrollingEnabled(false);
        }
 mWebView.loadDataWithBaseURL(
                            "file:///android_asset/",
                            content,
                            "text/html",
                            "utf-8",
                            null);

1 个答案:

答案 0 :(得分:0)

尝试在wrap_content内使用RelativeLayoutNestedScrollView个高度属性,如下面的代码

  <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/postDetails"
    android:background="?attr/backgroundColor">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> //change here

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:id="@+id/postFeaturedImage"/>

        <com.app.customizeviews.Lato_Bold_TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="?attr/primaryTextColor"
            android:letterSpacing="0.02"
            android:lineSpacingExtra="6sp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="16dp"
            android:id="@+id/postTitle"
            android:layout_below="@+id/postFeaturedImage"/>

        <com.app.customizeviews.Lato_Regular_TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/postDate"
            android:textColor="?attr/secondaryTextColor"
            android:textSize="14sp"
            android:layout_below="@+id/postTitle"
            android:layout_marginTop="8dp"
            android:layout_marginStart="16dp" />

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/postDate"
            android:layout_marginTop="12dp"/>

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="50dp"
            android:id="@+id/progressBar"
            android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>