【问题标题】:Scroll Webview inside a Scroll View在 Scroll View 中滚动 Webview
【发布时间】:2012-09-24 14:48:32
【问题描述】:

我有什么:
现在我有一个滚动视图作为父母。在这个滚动视图中,我使用了一个 WebView,它加载一个 URL,然后在其中显示文本。 这是我的 xml:

<ScrollView
    android:id="@+id/parentScroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Heading" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:background="@drawable/webview" />
    </RelativeLayout>
</ScrollView>    

我想要什么:
我想在里面滚动 webView。当我触摸 webView 时,不幸的是父滚动视图被调用。 我还必须保留父滚动视图,但除此之外,当我触摸 webView 时,我想滚动其中的 WebView 内容。

我该怎么做?

【问题讨论】:

    标签: android webview scrollview


    【解决方案1】:

    创建自定义触摸拦截 Web 视图

    CustomWebview.java

     package com.mypackage.common.custom.android.widgets
    
    public class CustomWebview extends WebView {
    
        public CustomWebview(Context context) {
            super(context);
        }
    
        public CustomWebview(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomWebview(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event){
            requestDisallowInterceptTouchEvent(true);
            return super.onTouchEvent(event);
        }          
    }
    

    layout.xml

    <com.package.custom.widgets.CustomWebview
                    android:id="@+id/view_extra"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                     />
    

    【讨论】:

      【解决方案2】:

      根据 Android 设计文档,如果滚动容器的滚动方向相同,则永远不应将滚动容器放入滚动容器中。它不是用来处理这种事情的。

      【讨论】:

      • @CaseyB... 但这是我的要求.. 我必须将 webView 保持在滚动视图中。你对此有什么想法吗??
      • 那恐怕你的要求是错误的,你永远不需要这样做。
      • 可以参考一下文档吗?
      • 我同意这一点并且厌倦了听这个教科书的评论,因此新的嵌套滚动标志和协调器布局,因为谷歌的 android 团队意识到做这样的通用评论不是要走的路和嵌套应该以有效的方式处理滚动。
      【解决方案3】:

      我遇到了同样的问题。您应该将 webView 高度设置为与其内容相等。为此,请执行以下操作:
      将此行添加到 Activity 中的 onCreate 方法中:

      webView.setWebViewClient(new WebViewClient() {
                  @Override
                  public void onPageFinished(WebView view, String url) {
                      webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
      
                      super.onPageFinished(view, url);
                  }
              });
      webView.addJavascriptInterface(this, "MyApp");
      

      并将此方法添加到您的活动中:

      @JavascriptInterface
          public void resize(final float height) {
              MainActivity.this.runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
                  }
              });
          }
      

      【讨论】:

        【解决方案4】:

        通常不建议使用嵌套的可滚​​动小部件。这是一种令人困惑的用户体验,因为滚动错误的内容很容易。假设我打算滚动外部滚动区域,但我先触摸了内部区域并且非常努力地一甩。然后内部的会滚动,我会喜欢,是吧?为什么不滚动?

        即使一个是水平滚动而另一个是垂直滚动,手势识别器也可能会混淆另一个,因此您会获得相同的效果。这是一个有效的用例,但它仍然存在问题,我会避免它。 (IE:人类不能完美地垂直和水平滑动,通常是有角度的。)

        我会推动更改设计以打破可滚动区域。理想情况下,每页 1 个可滚动项目。甚至自己提出一个,并提供给设计师两种体验,看看他们选择哪一种。

        表达这将是多么糟糕。看看这个例子。这不是解决方案,只是为了展示经验。

        <ScrollView
            android:id="@+id/parentScroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Heading" >
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:orientation="vertical"
            android:padding="5dp" >
            <TextView
                android:id="@+id/topText"
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:text="@string/lotsoftext" />
            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@drawable/webview" />
            <TextView
                android:id="@+id/topText"
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:text="@string/lotsoftext" />
        </RelativeLayout>
        

        【讨论】:

        • @Jermey Edwards .. 谢谢我的朋友这样的解释,但不幸的是这是我的要求..我必须将 webView 放在滚动视图中..这样当我点击 webView 时它应该垂直滚动并且什么时候我在 webView 外部单击,整个屏幕将滚动。你对此有什么想法吗??
        • @Jeremy Edwards 请看看我的解决方案,它确实提供了将 webview 放置在滚动视图中的解决方法here
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多