【问题标题】:How to have vertical scrolling in Webviews that are inside a Scrollview?如何在 Scrollview 内的 Webview 中进行垂直滚动?
【发布时间】:2012-06-21 13:03:41
【问题描述】:

我有一个嵌入在滚动视图中的 Web 视图。 Webview 本身具有可垂直滚动的区域。

现在如果我尝试在 webview 内滚动,scrollview 会拦截 touchevent 并滚动整个 webview,而不是只移动小的可滚动 div。

只有当 webview 不想滚动时,我怎样才能使滚动视图工作?

【问题讨论】:

  • 你好@Janusz 我面临同样的问题。你找到解决方案了吗?如果是,请发布您的答案

标签: android webview scrollview touch-event


【解决方案1】:

@Janusz,我也遇到了同样的问题。我的解决方案是基于扩展的滚动视图行为以及正确的布局。我已经写了同一个问题的答案here。 如果您有实施问题或疑问,请告诉我,请告知是否有帮助:)

【讨论】:

    【解决方案2】:

    在我的例子中,我在滚动视图容器中有一个 webview,并且滚动视图和 webview 是全屏的。我可以通过覆盖 webView touchListener 中的 onTouch 事件来解决这个问题。

       scrollView = (ScrollView)findViewById(R.id.scrollview); 
    
       webView.setOnTouchListener(new OnTouchListener() {           
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                scrollView.requestDisallowInterceptTouchEvent(true);
                return false;
            }
       }
    

    【讨论】:

      【解决方案3】:

      使用 TouchyWebView.java

      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);
          }          
      }
      

      在布局文件中:

      <yourclasapath.TouchyWebView 
                      android:id="@+id/description_web"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                       />
      

      【讨论】:

        【解决方案4】:

        我们的解决方案通过Javascript Interface 使用Javascript 回调。每次触摸 WebView 内可滚动的 UI 的一部分时,都会通过 java 脚本调用一个侦听器,并且该侦听器在 WebViews 父级上调用 requestDisallowInterceptTouchEvent。

        这不是最佳解决方案,但却是目前找到的最佳解决方案。如果用户滚动得非常快,WebView 中的图层将不会滚动,但以正常的滚动速率可以正常工作。

        【讨论】:

        • 您能否分享一些您使用的代码。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-22
        • 1970-01-01
        • 2017-04-17
        相关资源
        最近更新 更多