如何在ViewPager中托管的WebView中支持水平滚动?

时间:2019-02-21 10:42:12

标签: android webview

我需要在ViewPager中托管的webview中支持水平滚动。当我到达水平Web视图的末端时,我应该可以在ViewPager中滑动到上一个/下一个项目。

我尝试了以下代码。

   <android.support.v4.widget.NestedScrollView
    android:id="@+id/web_notify_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <HorizontalSwipeWebView
        android:id="@+id/web_item_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.v4.widget.NestedScrollView>

我正在扩展WebView类并覆盖onTouchEventonOverScrolled事件。

override fun onOverScrolled(scrollX: Int, scrollY: Int, clampedX: Boolean, clampedY: Boolean) {
    //Enables ViewPager when scroll reaches end.
    if (clampedX) {
        Log.d(TAG, "Web view over scrolled in X direction, so enabling horizontalScrollingParent paging back")
        horizontalScrollingParent?.requestDisallowInterceptTouchEvent(false)
        isScrolling = false
    }

    if (clampedY) {
        Log.d(TAG, "Web view over scrolled in Y direction, so enabling Vertical Parent back")
        verticalScrollingParent?.requestDisallowInterceptTouchEvent(false)
    }
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY)
}

override fun onTouchEvent(event: MotionEvent): Boolean {
    //disables ViewPager when user presses down

    val consumed = super.onTouchEvent(event)
    //gestureDetector.onTouchEvent(event)
    when (event.action) {
        MotionEvent.ACTION_DOWN -> {
            horizontalScrollingParent?.requestDisallowInterceptTouchEvent(true)
            verticalScrollingParent?.requestDisallowInterceptTouchEvent(true)
        }
        MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL,
        MotionEvent.ACTION_OUTSIDE -> {
            Logger.d(TAG, "Action event: ${event.action} -> so enabling parents intercept")
            horizontalScrollingParent?.requestDisallowInterceptTouchEvent(false)
            verticalScrollingParent?.requestDisallowInterceptTouchEvent(false)
            isScrolling = false
        }
    }

    return isScrolling || consumed
}

这可以正常工作,但是在使用Webview一段时间后,WebView变得太慢。还有其他方法吗?

0 个答案:

没有答案