CoordinatorLayout与Webview 滑动冲突

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="com.kenny.hurry.WebActivity">
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
android:id="@+id/id_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="10dp"
app:layout_scrollFlags="scroll|enterAlways"
app:navigationIcon="@drawable/selector_back"
app:titleTextColor="@color/primary_material_light" />

android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

android:id="@+id/wb_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

Webview内容可以滑动时,向上滑动Webview,Toolbar应该是要进行隐藏的,然而并没有
原本以为 是 SwipeRefreshLayout 和WebView 的冲突导致的 按照这个思路 差了好多资料 发现不能解决问题,后来有想到可能是CoordinatorLayout 和webview的问题
原因是因为滑动的并不是WebView本身,而是其加载网页的内容 所以Toolbar不会隐藏 把webview外面嵌套一层NestedScrollView 解决问题

或者用自定义的webview 也可以解决问题
public class NestedScrollWebView extends WebView implements NestedScrollingChild {

public static final String TAG = NestedScrollWebView.class.getSimpleName();

private int mLastMotionY;

private final int[] mScrollOffset = new int[2];
private final int[] mScrollConsumed = new int[2];

private int mNestedYOffset;

private NestedScrollingChildHelper mChildHelper;

public NestedScrollWebView(Context context) {
    super(context);
    init();
}

public NestedScrollWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public NestedScrollWebView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    mChildHelper = new NestedScrollingChildHelper(this);
    setNestedScrollingEnabled(true);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;

    MotionEvent trackedEvent = MotionEvent.obtain(event);

    final int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;
    }

    int y = (int) event.getY();

    event.offsetLocation(0, mNestedYOffset);

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionY = y;
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            result = super.onTouchEvent(event);
            break;
        case MotionEvent.ACTION_MOVE:
            int deltaY = mLastMotionY - y;

            if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                deltaY -= mScrollConsumed[1];
                trackedEvent.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }

            int oldY = getScrollY();
            mLastMotionY = y - mScrollOffset[1];
            if (deltaY < 0) {
                int newScrollY = Math.max(0, oldY + deltaY);
                deltaY -= newScrollY - oldY;
                if (dispatchNestedScroll(0, newScrollY - deltaY, 0, deltaY, mScrollOffset)) {
                    mLastMotionY -= mScrollOffset[1];
                    trackedEvent.offsetLocation(0, mScrollOffset[1]);
                    mNestedYOffset += mScrollOffset[1];
                }
            }

            trackedEvent.recycle();
            result = super.onTouchEvent(trackedEvent);
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            stopNestedScroll();
            result = super.onTouchEvent(event);
            break;
    }
    return result;
}


@Override
public void setNestedScrollingEnabled(boolean enabled) {
    mChildHelper.setNestedScrollingEnabled(enabled);
}

@Override
public boolean isNestedScrollingEnabled() {
    return mChildHelper.isNestedScrollingEnabled();
}

@Override
public boolean startNestedScroll(int axes) {
    return mChildHelper.startNestedScroll(axes);
}

@Override
public void stopNestedScroll() {
    mChildHelper.stopNestedScroll();
}

@Override
public boolean hasNestedScrollingParent() {
    return mChildHelper.hasNestedScrollingParent();
}

@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
    return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}

@Override
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
    return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}

@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
    return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}

@Override
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
    return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}

}

你可能感兴趣的:(CoordinatorLayout与Webview 滑动冲突)