2015-08-08 31 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