Android-向下滚动时WebView刷新

时间:2019-08-13 13:22:24

标签: java android android-activity android-webview swiperefreshlayout

在现阶段,这可能是一个愚蠢的问题,但我一直在网上浏览线程,到目前为止,没有一种解决方案对我有用。

我有一个活动,其中一个Webview嵌套在SwipeRefreshLayout内,以启用“推入刷新”功能来刷新页面。问题出现在当我尝试滚动到网页顶部时,调用OnRefresh回调。以下是一些我尝试解决的相关代码的片段。我尝试实现ViewTreeObserver.OnScrollChangedListener接口以在滚动条位于顶部时启用刷新功能。我想知道我是否想念一些东西。

Browser.java

public class Browser extends AppCompatActivity implements Common, ViewTreeObserver.OnScrollChangedListener {
    private final String TAG = getClass().getSimpleName();
    private ProgressBar loadingPageBar;
    private Toolbar toolbar;
    private WebView browser;
    private WebSettings webSettings;
    private SwipeRefreshLayout refreshLayout;
    private String urlLink;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser);
        Log.i(TAG, "onCreate:  Called");

        declareVariables();
        getIntentData();

        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        toolbar.setNavigationOnClickListener(view -> onBackPressed());
        toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
        toolbar.setSelected(true);

        webSettings = browser.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setSaveFormData(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        browser.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                refreshLayout.setRefreshing(false);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        browser.setWebChromeClient(new ChromeClient());
        enableHtml5Features();
        browser.clearCache(true);
        browser.loadUrl(urlLink);

        refreshLayout.setRefreshing(true);
        refreshLayout.getViewTreeObserver().addOnScrollChangedListener(this);
        refreshLayout.setOnRefreshListener(() -> browser.reload());

    }

    @Override
    public void onScrollChanged() {
        System.out.println(browser.getScrollY());
        if (browser.getScrollY() == 0)
            refreshLayout.setEnabled(true);
        else
            refreshLayout.setEnabled(false);
    }                   
}

activity_browser.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Browser">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/webview_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ColoredBackArrow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view3"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/greyDark"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/webview_toolbar" />

    <ProgressBar
        android:id="@+id/loadingBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        app:layout_constraintBottom_toBottomOf="@id/webview_toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.473"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/view3">

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fitsSystemWindows="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view3" />
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

添加SwipeRefreshLayoutListener,例如setOnRefreshListener,如下所示:

 refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(true);
                loadWebView();
            }
        });

带有SwipeRefreshLayout布局的整个类:

public class WebViewActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();

    private WebSettings webSettings;
    private View view3;
    private ProgressBar loadingBar;
    private SwipeRefreshLayout refreshLayout;
    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        view3 = findViewById(R.id.view3);
        loadingBar = findViewById(R.id.loadingBar);
        refreshLayout = findViewById(R.id.swipeContainer);
        webview = findViewById(R.id.webview);

        webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setSaveFormData(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        loadWebView();

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(true);
                loadWebView();
            }
        });

    }
    private void loadWebView(){
        String urlLink="https://stackoverflow.com/questions/57478675/android-webview-refreshing-when-scrolling-down";
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                refreshLayout.setRefreshing(false);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        webview.setWebChromeClient(new WebChromeClient());
        webview.clearCache(true);
        webview.loadUrl(urlLink);
    }
}

希望它对您有用。