Android Webview in Viewpager with enabled Hardware Acceleration

1.5k views Asked by At

I have a ViewPager containing a WebView. After some lacks of scroll performance and consulting StackOverflow, I finally disabled hardware acceleration for the WebView. This solves the problem, but now I need to display Youtube videos and other multimedia stuff in my WebView. According to the dokumentation, I need to re-enable hardware acceleration for this :(
Because the scroll issue appears again, I added a ViewPager.OnPageChangeListener() to my ViewPager where I disable hardware acceleration when the pager is scrolling and enable it, when the pager is idle:

pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if(arg0 == ViewPager.SCROLL_STATE_IDLE){

                    webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                }
                else{
                    // disable hardware acceleration
                    webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                }
            }
        });

It seems to work a bit, but when the hardware acceleration is enabled, the WebView flickers a short time, which is annoying. I have no idea what I can try next... any sugestions?

1

There are 1 answers

0
seyfullah.bilgin On

You should add webview inside the NestedScrollView. In this way i can scroll any html content such as youtube video in the webview without lack of scroll performance.

  <androidx.core.widget.NestedScrollView
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

    </androidx.core.widget.NestedScrollView>