ScrollView中的Android WebView仅滚动scrollview

Pan*_*nos 32 android scroll scrollview webview

在我的应用程序中,我有一个ScrollView,其中包含一些线性视图,一些textview和一个Webview,然后是其他线性布局等.问题是WebView不会滚动.Scroll仅侦听ScrollView.有什么建议??


<ScrollView >
    <TextView />
    <WebView />              <-- this does not scroll
    <TextView />
</ScrollView >
Run Code Online (Sandbox Code Playgroud)

Pan*_*nos 82

这是解决方案.在网上找到.我已经将WebView子类化,我正在使用该requestDisallowInterceptTouchEvent(true);方法来允许我的webview处理滚动事件.

TouchyWebView.java

package com.mypackage.common.custom.android.widgets

public class TouchyWebView extends WebView {

    public TouchyWebView(Context context) {
        super(context);
    }

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

    public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        requestDisallowInterceptTouchEvent(true);
        return super.onTouchEvent(event);
    }          
}
Run Code Online (Sandbox Code Playgroud)

并在layout.xml中

<com.mypackage.common.custom.android.widgets.TouchyWebView 
                android:id="@+id/description_web"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 />
Run Code Online (Sandbox Code Playgroud)

  • 一旦你触摸到`WebView`之外,这就会中断. (4认同)
  • 一直在四处寻找能够实现良好的webview/scrollview交互的东西,这似乎是最好的.在调用requestDisallowInterceptTouchEvent(true)方法之前,我在onTouchEvent()中添加了对"if(event.getPointerCount()> 1)"的检查.这允许整个页面垂直滚动,webview水平滚动,并且捏合和缩放以仅应用于webview. (3认同)

Dip*_*dra 11

@panos提供的解决方案有效但与ScrollView一起使用时仍然存在问题.以下增强版本克服了该问题.

public class TouchyWebView extends WebView {

public TouchyWebView(Context context) {
  super(context);
}

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

public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
}

    @Override
    public boolean onTouchEvent(MotionEvent event) {

  //Check is required to prevent crash
  if (MotionEventCompat.findPointerIndex(event, 0) == -1) {
    return super.onTouchEvent(event);
  }

  if (event.getPointerCount() >= 2) {
    requestDisallowInterceptTouchEvent(true);
  } else {
    requestDisallowInterceptTouchEvent(false);
  }

  return super.onTouchEvent(event);
}

@Override
protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
  super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
  requestDisallowInterceptTouchEvent(true);


 }

  }
Run Code Online (Sandbox Code Playgroud)

此外,您可能希望为TouchyWebView进行以下设置.

mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
Run Code Online (Sandbox Code Playgroud)

  • 注意 `MotionEventCompat.findPointerIndex(event, 0)` 现在已被弃用,我们应该使用 `event.findPointerIndex(0)` 代替。 (3认同)
  • 使用您的解决方案,`WebView` 只能通过两根手指滚动,只有一根手指可以滚动父级......不是那么直观:/ (2认同)

sna*_*msm 6

除了一个例外,Panos 解决方案对我来说就足够了......我的固定高度(200dp)WebView可能是空的,或者可能已经加载了很多内容。所以它可能是也可能不是“本身”可滚动的。Panos 解决方案MotionEvent总是消耗s,因此当WebView为空并且用户触摸WebView并尝试滚动时,WebView将不会滚动(因为没有内容)并且可滚动的父级也会导致WebView“吞咽” MotionEvent- 所以什么也没有发生。我if为预期行为添加了小声明:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(computeVerticalScrollRange() > getMeasuredHeight())
        requestDisallowInterceptTouchEvent(true);
    return super.onTouchEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
  • WebView为空且不可垂直滚动时computeVerticalScrollRange() == getMeasuredHeight()
  • WebView内容长于其高度(可滚动)时computeVerticalScrollRange() > getMeasuredHeight()