在ViewPager, ScrollView, ListView等容器控件中内嵌了webview,特别是webview的大小固定的情况下,会出现滑动的冲突,可以采用如下自定义的webview来完美解决。
当webview滑动到边界到时候,ViewPager,ListView或ScrollView才可以滚动
package com.leo.demo;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class NestWebView extends WebView {
// 是否垂直滚动
boolean isScrollV;
// 是否水平滚动
// boolean isScrollH;
public NestWebView(Context context) {
super(context);
init();
}
public NestWebView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public NestWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
WebSettings settings = getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.setMediaPlaybackRequiresUserGesture(false);
}
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setSupportMultipleWindows(false);
setWebChromeClient(new WebChromeClient());
setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (MotionEventCompat.getPointerCount(event) == 1) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isScrollV = false;
//isScrollH = false;
//事件由webview处理
getParent().getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
// 垂直viewpager,scrollview,listview等
getParent().getParent().requestDisallowInterceptTouchEvent(!isScrollV);
//嵌套Viewpager时
// getParent().getParent().requestDisallowInterceptTouchEvent(!isScrollH);
break;
default:
getParent().getParent().requestDisallowInterceptTouchEvent(false);
}
} else {
//使webview可以双指缩放(前提是webview必须开启缩放功能,并且加载的网页也支持缩放)
getParent().getParent().requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(event);
}
//当webview滚动到边界时执行
@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
isScrollV = clampedY;
//isScrollH = clampedX;
}
}