如何通过 scrollview 滚动 webview 的内容?

How can I scroll the webview's content by scrollview?

我在 webview 中加载了一个 HTML 页面,它可以通过 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>

为什么不在 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>

如果您希望所有视图都在单个滚动条中滚动,有一个非常有用的库 - commonsguy/cwac-merge

您需要满足一个条件才能使用下面提供的代码。请记住,这种解决问题的方法可能有所不同,但最终结果正是您所需要的。

条件:

  1. 此库只能与 ListView 一起使用。

就这些,让我们开始吧。

Make your activity layout this:

<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>

Create a new layout XML. Let it be named viewWebView.xml and paste this code:

<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>

Create a new layout XML. Let it be named viewTextView.xml and paste this code:

<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>

So, now we have to work with java file. Open your *Activity.java file and usage of library is below.

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

要记住的事情:

  1. MergeAdpater 下的视图将按照您向其中添加视图的方式添加。换句话说,您的顺序 mg.addView(View) 将是项目在 ListView 中出现的顺序。

干杯!如果您在实施任何东西或什至将库添加到 gradle 时遇到任何问题,请告诉我!

祝你好运!