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



我在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>

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

注意:ObservableWebView 使用 WebView 进行了扩展。

终于得到了解决方案,但通过使用安卓中的安卓"工具栏"。下面是新的布局文件:

<?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));
}
});

最新更新