How to fix WebView autoscroll to site's top after closing a fullscreen video mode? (There're code and examples)

141 Views Asked by At

Example on YouTube for understanding I have onShowCustomView for opening any videos in full screen mode. But after closing full screen, it autoscrolls to site's top

According to this ask, I added int positionY and it successfully done. But in global browsers it's not to do with "webView.postDelayed(() -> webView.scrollTo(0, positionY), 200)" =) So if it's possible, could you help me, please, to solve this problem better

private class MyChrome extends WebChromeClient{
    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;
    int positionY;

    MyChrome() {}

    public Bitmap getDefaultVideoPoster(){
        if (mCustomView == null){
            return null;
        }
        return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView(){
        ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;
        // Delay the scrollTo to make it work
        webView.postDelayed(() -> webView.scrollTo(0, positionY), 200);
    }

    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback){
        if (this.mCustomView != null){
            onHideCustomView();
            return;
        }
        positionY = webView.getScrollY();
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }
}
0

There are 0 best solutions below