2012-09-24 64 views
5

我有:
現在我有一個滾動視圖作爲父項。在這個滾動視圖中,我使用一個WebView加載一個URL,然後在其中顯示文本。 這裏是我的xml:在滾動視圖中滾動Webview

<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內容。

我該怎麼做?

回答

4

根據Android設計文檔,如果滾動容器的滾動方向相同,則不應將滾動容器放在滾動容器中。這並不意味着要處理這樣的事情。

+2

@CaseyB ...但它我的要求。 。我必須保持webView在滾動視圖。你對這個有什麼想法嗎? – Noman

+2

那麼恐怕你的要求是錯誤的,你應該永遠不需要這樣做。 –

+0

@JosephEarl ..好吧,然後我嘗試改變設計。它對我來說很難.. – Noman

0

通常不建議嵌套的可滾​​動窗口小部件。這是一個令人困惑的用戶體驗,因爲很容易滾動錯誤的東西。假設我打算滾動外滾動區域,但我首先觸摸了內部區域,並且非常硬。那麼內在的一個會滾動,我會像?爲什麼它不滾動?

即使一個水平滾動而另一個垂直,手勢識別器可能會混淆另一個,因此您會得到相同的效果。這是一個有效的用例,但它仍然是有限的,我會避免它。 (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> 

+0

@Jermey愛德華茲..謝謝你我的朋友這樣的解釋,但不幸的是我的要求..我必須把webView內滾動視圖..所以,當我點擊webView它應該垂直滾動,當我點擊外部webView的whoel屏幕應滾動。 你對此有什麼想法嗎? – Noman

+0

@Jeremy Edwards請看我的解決方案,它確實提供了將webview放置在scrollview中的解決方法[here](http://stackoverflow.com/a/12963499/1152250) – rus1f1kat0R

6

layout.xml創建自定義觸摸攔截網頁視圖

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

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

我有同樣的問題。您應該將webView高度設置爲與其內容相同。該這樣做:
此行添加到您的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))); 
      } 
     }); 
    }