在使用Webview進行滑動操做時,從屏幕可見區域外向內滑動時,會出現webview區域閃爍的問題(反之也是),本文將提供一種解決方案。javascript
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:overScrollMode="never"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@color/colorPrimary" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/contract_font"></WebView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
複製代碼
能夠看到,NestedScrollView嵌套webview,且webview初始未在一屏內時,滑進出屏幕時會有短暫的白色塊。前端
方案 | 考慮點 |
---|---|
android:hardwareAccelerated="false" | 5.0 開始Android系統爲了充分利用GPU的特性,使得界面渲染更加平滑而默認開啓的,若是關掉的話,那麼整個網頁不流暢了,豈不是得不償失——>放棄 |
setBackgroundColor(Color.parseColor(「#00000000」)); setBackgroundResource(R.drawable.white); | 設置底色背景,可是webview自己是加載的H5頁面,使用的是H5頁面的底色背景,並且經過上面的gif能夠看出,沒有效果——>放棄 |
==經過樣式佈局,讓webview保持在第一屏內初始化== | 本文嘗試的方案 |
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:overScrollMode="never"
android:scrollbars="none">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/contract_font"></WebView>
<View
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@color/colorPrimary" />
</FrameLayout>
</android.support.v4.widget.NestedScrollView>
複製代碼
經過FrameLayout來疊加使得webview保持在第一屏內初始化,而後設置webview的padding,這樣使得完整的H5內容是在ContentView下方顯示。 可是——>webview設置padding根本無效!!!java
怎麼辦呢?不管怎樣也想不到爲何會如此,畢竟自己api的實現上是有些缺陷的(https://stackoverflow.com/questions/9170042/how-to-add-padding-around-a-webview )android
最終的解決方案則是經過注入js代碼來控制H5的padding來解決。web
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
contentView.post(new Runnable() {
@Override
public void run() {
contentViewHeight = px2dp(getApplicationContext(), contentView.getMeasuredHeight());
if (contentViewHeight > 0) {
webView.loadUrl("javascript:document.body.style.marginTop=\"" + contentViewHeight + "px\"; void 0");
}
}
});
}
});
複製代碼
看下猜測運行的結果: api
打開網頁編輯模式,查看body這塊的樣式: 網絡
只須要將這部分操做轉換爲對應的代碼便可: 將上面的 webView.loadUrl("javascript:document.body.style.paddingTop="" + contentViewHeight + "px"; void 0"); 替換爲:ide
webView.loadUrl("javascript:document.body.style.marginTop=\"" + contentViewHeight + "px\"; void 0");
複製代碼
整個方案的實現其實就兩塊: 1.佈局,讓webview在一屏內初始; 2.設置H5網頁的margin-top或者padding-top;佈局