滚动网页视图时如何隐藏工具栏?

时间:2020-03-27 18:19:53

标签: android webview

我有一个像这样的XML文件:

<androidx.constraintlayout.widget.ConstraintLayout 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">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar_main"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_constraintTop_toTopOf="parent" />

<WebView
    android:id="@+id/webview_main"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar_main" />

</androidx.constraintlayout.widget.ConstraintLayout>

滚动网页视图时,我想隐藏工具栏。因此,编写以下代码:

webView.setOnScrollChangedCallback(new CustomWebView.OnScrollChangedCallback() {
        @Override
        public void onScroll(int l, int t, int oldl, int oldt) {
           if (t > oldt) {
                toolbar.animate().translationY(-actionBarSize).setDuration(200);
                toolbar.setVisibility(View.GONE);
            } else if (t < oldt) {
                toolbar.animate().translationY(0).setDuration(200);
                toolbar.setVisibility(View.VISIBLE);
            }
        }
    });

因此,当我滚动Web视图时,页面滚动会滞后很多。

当我从代码中删除 toolbar.setVisibility(...)时,滞后问题得到了解决,但是,当工具栏消失时,屏幕顶部会出现一个空白。 / p>

如何解决此问题?

0 个答案:

没有答案
相关问题