简体   繁体   English

如何在scrollView中添加WebView滚动

[英]How to add webview scroll in an scrollView

I have a requirement to show multiple items in a list and add a WebView in the end. 我需要在列表中显示多个项目,并在最后添加一个WebView。

I have added a RecyclerView and a WebView, and put both of them in a ScrollView. 我添加了RecyclerView和WebView,并将它们都放入ScrollView中。 This is working fine, but it does not shows a scroll in webview. 这工作正常,但是在webview中没有显示滚动。

I need my webview to have a vertical scroll. 我需要Web视图具有垂直滚动。

This is my code. 这是我的代码。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Title"
    >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvMatchday"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <WebView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_gravity="bottom"
            android:scrollbars="vertical"
            android:background="@color/black"
            android:id="@+id/twitterWebView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rvMatchday" />


    </LinearLayout>

</ScrollView>

Then I created a parent layout file, and added above layout in that. 然后,我创建了一个父布局文件,并在其中添加了上面的布局。

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

   <include layout="@layout/fragment_match"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</ScrollView>

在您的活动/片段上,将您的网络视图设置为可滚动

webView.setVerticalScrollBarEnabled(true);

I think you don't need to use scroll view as root because recycler view and web view having their own scrolling effects. 我认为您不需要以滚动视图为根,因为回收者视图和Web视图具有自己的滚动效果。 Just try with Linear as root 只需尝试使用Linear作为根

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvMatchday"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <WebView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="@color/black"
        android:id="@+id/twitterWebView"
       />


</LinearLayout>

You don't need to use Scroll view in your layout.xml file as well as parent layout file. 您不需要在layout.xml文件和父布局文件中使用“滚动”视图。 like 喜欢

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvMatchday"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <WebView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_gravity="bottom"
            android:scrollbars="vertical"
            android:background="@android:color/black"
            android:id="@+id/twitterWebView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rvMatchday" />
             </LinearLayout>

and parent layout 和父布局

    <android.support.constraint.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:fillViewport="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <include layout="@layout/fragment_match"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM