首先,自定义WebView
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.webkit.WebView;
public class CollapsingWebView extends WebView {
private GestureDetector detector;
private boolean isScrollBottom = false;
public CollapsingWebView(Context context) {
this(context, null);
}
public CollapsingWebView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CollapsingWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
detector = new GestureDetector(context, new SimpleOnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
if (!isScrollBottom) {
requestDisallowInterceptTouchEvent(true);
} else {
isScrollBottom = false;
}
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
float webcontent = getContentHeight() * getScale();
float webnow = getHeight() + getScrollY();
isScrollBottom = (Math.abs(webcontent - webnow) < 1);
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
return super.onTouchEvent(event);
}
}
之后把WebView嵌套在AppBarLayout或CollapsingToolbarLayout里就不会冲突了
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context=".ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="280dp"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.myapplication2.CollapsingWebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.myapplication2.CollapsingWebView>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling" />
</android.support.design.widget.CoordinatorLayout>