Webview 在nestedscrollview 中无法正常工作

问题描述

我在 nestedScrollView 中有一些内容,在此内容我有 WebView,高度为 wrap_content。当我在 WebView 内的元素上滚动 nestedScrollView 时滚动不起作用。

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <androidx.core.widget.nestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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


            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_launcher_foreground" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/ic_launcher_foreground" />

            <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

            <WebView
                android:id="@+id/widgets"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
    </androidx.core.widget.nestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

WebView inside NestedScrollView (not work)

WebView without NestedScrollView

解决方法

        public class CustomView extends WebView {
        
            public CustomView(Context context) {
                super(context);
            }
        
            public CustomView(Context context,AttributeSet attrs) {
                super(context,attrs);
            }
        
            public CustomView(Context context,AttributeSet attrs,int defStyle) {
                super(context,attrs,defStyle);
            }
        
            @Override
            public boolean onTouchEvent(MotionEvent event){
                requestDisallowInterceptTouchEvent(true);
                return super.onTouchEvent(event);
            }          
        }
        
        
        <com.abc.customer.android.widgets.CustomView 
                        android:id="@+id/my_id"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                         />
    
    
    Structure looks like
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.main.view.MainActivity">
    
      
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:fillViewport="true"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    app:srcCompat="@drawable/abc_vector_test" />
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    app:srcCompat="@drawable/abc_vector_test" />
    
                <Button
                    android:id="@+id/button"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Button" />
    
                <WebView
                    android:id="@+id/webview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
                    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    app:srcCompat="@drawable/abc_vector_test" />
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    app:srcCompat="@drawable/abc_vector_test" />
                    
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    
    </RelativeLayout>




in backend 

    private lateinit var webView: WebView



   webView = findViewById(R.id.webview)
        webView.settings.setJavaScriptEnabled(true)

        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?,url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
        webView.loadUrl("https://www.google.co.in/")