簡體   English   中英

當我在swiprefreshlayout和nestedscrollview中使用webview時,如何刪除webview底部的空白區域?

[英]How can I remove white space on bottom of webview when i use webview in swiprefreshlayout and nestedscrollview?

我正在研究WebView項目,我想將WebView與SwipRefreshLayout一起使用,並且為了滾動Webview,我必須將WebView放在NestedScrollView中,但是WebView在html內容中添加帶有邊框的不需要的空格,而當我設置setJavaScriptEnabled時(false),WebView正常工作。 我已嘗試過網絡上的所有解決方案,但我無法解決。

activity_main

<?xml version="1.0" encoding="utf-8"?><!--xmlns:ads="http://schemas.android.com/apk/res-auto"-->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_id"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:popupTheme="@style/PopupTheme"
                android:fitsSystemWindows="true"
                android:theme="@style/ToolbarTheme"
                app:subtitleTextAppearance="@style/Toolbar.SubTitleText"
                app:title="Toolbar"
                app:titleTextAppearance="@style/Toolbar.TitleText"
                app:titleTextColor="#ffffff"
                app:layout_scrollFlags="scroll|enterAlways"
                />

        </android.support.design.widget.AppBarLayout>
        <RelativeLayout
            android:id="@+id/refresh_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            >

            <ImageView
                android:id="@+id/refreshIV"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_centerInParent="true"
                android:src="@drawable/refresh_button"
                 />

        </RelativeLayout>

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >


            <android.support.v4.widget.NestedScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                >


                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"

                    >

                    <!--&gt;-->
                    <FrameLayout

                        android:id="@+id/progressbar_container"
                        android:layout_width="match_parent"
                        android:layout_height="4dp">

                        <ProgressBar
                            android:id="@+id/progressbar_id"
                            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="8dp"
                            android:scaleY="2"
                            android:progressDrawable="@drawable/progresscolor"
                            android:max="100" />
                    </FrameLayout>
                    <FrameLayout

                        android:id="@+id/refresh_page_container"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="#f2f2f2"
                        android:visibility="gone"
                        >
                        <ImageView
                            android:id="@+id/refresh_btn_iv"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_refresh"
                            android:layout_gravity="center"
                            />
                    </FrameLayout>

                    <FrameLayout
                        android:id="@+id/webview_view"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"

                        >

                        <WebView
                            android:id="@+id/main_webView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fitsSystemWindows="true"

                            />
                    </FrameLayout>

                    <FrameLayout
                        android:id="@+id/target_view"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:visibility="gone">
                    </FrameLayout>
                </FrameLayout>
            </android.support.v4.widget.NestedScrollView>


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

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:visibility="gone"
            ads:adSize="BANNER"
            ads:adUnitId="@string/test_unit_id" />
        <!--</FrameLayout>-->


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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/toolbar_header"
        app:menu="@menu/navigation_menu" />
</android.support.v4.widget.DrawerLayout>

經過幾天的研究,我找到了解決方案。

在這種情況下,我們可以從布局中刪除NestedScrollView並使用OnScrollChangeListener來控制WebView的滾動。 當然,自API 23+以來我們可以直接使用它,但對於API <23,我們的行為如下:

ObservableWebView

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class ObservableWebView extends WebView {
    private OnScrollChangedCallback mOnScrollChangedCallback;

    public ObservableWebView(final Context context)
    {
        super(context);
    }

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

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

        @Override
    protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt)
    {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mOnScrollChangedCallback != null) mOnScrollChangedCallback.onScroll(l, t, oldl, oldt);
    }

    public OnScrollChangedCallback getOnScrollChangedCallback()
    {
        return mOnScrollChangedCallback;
    }

    public void setOnScrollChangedCallback(final OnScrollChangedCallback onScrollChangedCallback)
    {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }

    /**
     * Implement in the activity/fragment/view that you want to listen to the webview
     */
    public static interface OnScrollChangedCallback
    {
        public void onScroll(int l, int t, int oldl, int oldt);
    }
}

並且您需要實現OnScrollChangedCallback接口,然后檢查webview的scrollY位置以啟用或禁用swiperefresh:

ObservableWebView webView= findViewById(R.id.main_webView);
    /**
     *
     * @param l    Current horizontal scroll position.
     * @param t    Current vertical scroll position.
     * @param oldl Previous horizontal scroll position.
     * @param oldt Previous vertical scroll position.
     */
webView.setOnScrollChangedCallback(new OnScrollChangedCallback(){
    public void onScroll(int l, int t, int oldl, int oldt){
                if (t == 0) {
                    swiperefresh.setEnabled(true);
                } else {
                    swiperefresh.setEnabled(false);
                }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM