Как я могу удалить пробелы в нижней части веб-просмотра, когда я использую веб-просмотр в swiprefreshlayout и nestedscrollview? - PullRequest
0 голосов
/ 23 апреля 2019

Я работаю над проектом 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>

1 Ответ

0 голосов
/ 24 апреля 2019

После нескольких дней исследований я нашел решение.

В этом случае мы можем удалить NestedScrollView из макета и использовать OnScrollChangeListener для управления прокруткой WebView. Конечно, с API 23+ мы можем использовать его напрямую, но для API <23 мы действуем следующим образом: </p>

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, а затем проверить позицию 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);
                }
    }
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...