2015-08-08 80 views
0

我在webview中加載一個HTML頁面,它可以通過webview滾動內容,但我想滾動根視圖。有什麼建議?如何通過scrollview滾動webview的內容?

這裏是我的佈局代碼:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:fillViewport="true"> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/layout_background"> 

     ... 

      <WebView 
       android:id="@+id/webView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:descendantFocusability="blocksDescendants" 
       android:nestedScrollingEnabled="false"> 
      </WebView> 

    </RelativeLayout> 
</ScrollView> 
+0

要幫助你的人,這個問題需要清除。你的問題不清楚。那麼,爲什麼你想要滾動根視圖?或者你想從滾動rootView中獲得什麼?因爲視圖無論如何都是可滾動的。 – Want2bExpert

+1

將Scrollable視圖放入另一個Scrollable視圖是一種**糟糕的做法。 –

+0

因爲我有其他意見,並且webview不夠高 – LichFaker

回答

0

如果你希望所有的意見,得到滾動單滾輪裏面,有一個非常有用的庫 - commonsguy/cwac-merge

有你需要Ø滿足使用單個條件代碼如下。請記住,針對您的問題的這種方法可能會有所不同,但最終結果正是您需要的。

條件:

  1. 這個庫只能與ListView的使用。

這就是所有,讓我們開始。

讓你的活動佈局這樣的:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:background="#FFFFFF"> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</ListView> 
</RelativeLayout> 

創建一個新的佈局XML。讓它被命名爲viewWebView.xml並粘貼 此代碼:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 


    <WebView 
     android:id="@+id/webView" 
     android:layout_width="match_parent" 
     android:layout_height="120dp" 
     android:layout_alignParentBottom="true" 
     android:background="#767676"> 
    </WebView> 

</RelativeLayout> 

創建一個新的佈局XML。讓它被命名爲viewTextView.xml並粘貼 此代碼:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 


    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello There" 
     android:textColor="#FFFFFF"> 

    </TextView> 

</RelativeLayout> 

所以,現在我們有一個Java文件的工作。打開你的* Activity.java文件 和庫的用法如下。

ListView list = (ListView) findViewById(R.id.list); 
MergeAdapter mg = new MergeAdapter(); 

/** 
* Prgram your webview 
*/ 
View web = getLayoutInflater().inflate(R.layout.viewWebView, null); 
WebView web = (WebView) web.findViewById(R.id.webView); 
//TODO: Code your webview!! 

mg.addView(web); 
mg.addView(getLayoutInflater().inflate(R.layout.viewTextView, null)); 
list.setAdapter(mg); 

觀光Rememeber:MergeAdpater

  1. 意見將在您添加視圖的方式加入。換句話說,您的順序是mg.addView(View)將是ListView中項目出現的順序。

乾杯!如果您在執行任何操作時遇到任何問題,或者將庫添加到Gradle中,請讓我知道!

祝你好運!

+0

非常感謝!這對我很有幫助 – LichFaker

+0

做一個贊成並接受讓他人知道它解決了你的問題的答案。 – Gavin

0

爲什麼不加你WebView上述RelativeLayout內滾動內容?你可以做這樣的事情:

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:background="#FFFFFF"> 

<WebView 
    android:id="@+id/webView" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:layout_alignParentBottom="true" 
    android:background="#767676"> 
</WebView> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_above="@id/webView" 
    android:background="#222222"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello There" 
     android:textColor="#FFFFFF"> 

    </TextView> 

</ScrollView> 
</RelativeLayout> 
+0

因爲在我的項目中,webview必須在佈局的底部 – LichFaker