在WebView的情况下尝试隐藏和显示布局时闪烁的屏幕

时间:2018-03-16 20:05:32

标签: android android-webview android-animation

我在WebView上方有一个LinearLayout,我希望在用户使用动画向上和向下滚动时显示和隐藏此布局。问题是在使用以下代码隐藏和显示布局时,屏幕显示闪烁行为。代码是:

webView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
            @Override
            public void onScroll(int l, int t,int oldl, int oldt) {

                if(t > oldt+20){
                    System.out.println("Swipe UP");

                    if(topLayout.getVisibility() == View.GONE)
                        return;

                    Animation animation = new TranslateAnimation(0, 0,0, 500); //May need to check the direction you want.
                    animation.setDuration(1000);
                    animation.setFillAfter(true);
                    topLayout.startAnimation(animation);
                    topLayout.setVisibility(View.GONE);

                }
                else if(t < oldt-20){
                    System.out.println("Swipe Down");

                    if(topLayout.getVisibility() == View.VISIBLE)
                        return;

                    Animation animation = new TranslateAnimation(500, 0,500, 0); //May need to check the direction you want.
                    animation.setDuration(1000);
                    animation.setFillAfter(true);
                    topLayout.startAnimation(animation);
                    topLayout.setVisibility(View.VISIBLE);

                }
                Log.d(DEBUG_TAG,"After Scrolling "+String.valueOf(l)+" "+String.valueOf(t)+" "+String.valueOf(oldl)+" "+String.valueOf(oldt));
            }
        });

这是.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"

    tools:context="logger.ap.webviewer.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/top_Layout"
        android:orientation="horizontal">
        <EditText
            android:layout_width="0dp"
            android:layout_weight="9"
            android:hint="Enter URL...."
            android:id="@+id/edt_url"
            android:maxLines="1"
            android:inputType="text"
            android:layout_height="wrap_content" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/btn_hit"
            android:text="Send"/>
    </LinearLayout>


    <logger.ugi.webviewer.ObservableWebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/WebView" />

</LinearLayout>

在寻找解决方案时,我发现不是创建一个LinearLayout而是通过使用“ActionBar”解决了这个问题,但仍然出于好奇心,我想知道解决方案,以便将来可以帮助其他人。

注意:ObservableWebView使用WebView扩展。

1 个答案:

答案 0 :(得分:1)

终于得到了解决方案但是使用了android&#34; Toolbar&#34;在android中。这是新的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:id="@+id/activity_main"

    tools:context="logger.ap.webviewer.MainActivity">


    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ababab"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter URL...."
            android:id="@+id/edt_url"
            android:maxLines="1"
            android:inputType="text"
            android:layout_marginRight="20dip"
            android:padding="5dp"
            android:background="@drawable/custom_edit"
             />
    </android.support.v7.widget.Toolbar>

    <logger.ugi.webviewer.ObservableWebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?android:attr/actionBarSize"
        android:clipToPadding="false"
        android:scrollbarStyle="outsideOverlay"
        android:id="@+id/WebView" />

</LinearLayout>

问题是我正在做一个错误的检查,上下滚动导致闪烁,因为这是一种快速检查,我用工具栏得到的解决方案是:

webView.setOnScrollChangedCallback(new ObservableWebView.OnScrollChangedCallback() {
            @Override
            public void onScroll(int l, int t, int oldl, int oldt) {

                if (t > mActionBar.getHeight() && mActionBar.isShowing()) {
                    System.out.println("Swipe UP");

                    mActionBar.hide();

                } else if (t == 0 && !mActionBar.isShowing()) {
                    System.out.println("Swipe Down");

                    mActionBar.show();

                }
                Log.d(DEBUG_TAG, "After Scrolling " + String.valueOf(l) + " " + String.valueOf(t) + " " + String.valueOf(oldl) + " " + String.valueOf(oldt));
            }
        });