Webview汉堡菜单未展开

时间:2020-07-21 15:27:57

标签: java android

它曾经可以工作。网站上有一些更新,我对代码做了一些更新。然后,它停止工作,我不知道如何解决它。当您单击汉堡菜单时,它会像预期的那样变成X,但不会扩展到菜单中。

我更改的代码是将其放在Nestedscrollview中,因为swiperefresh不允许您向上滚动。但是,即使我删除了它,它似乎也不起作用。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:windowSoftInputMode="adjustResize">

        <androidx.core.widget.NestedScrollView
            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"
                android:windowSoftInputMode="adjustResize"
                app:layout_constraintDimensionRatio="1" />
        </androidx.core.widget.NestedScrollView>

    </RelativeLayout>

MainActivity.java

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    SwipeRefreshLayout mySwipeRefreshLayout;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBar = getSupportActionBar();
        actionBar.hide();
        mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);
        webView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings= webView.getSettings();
        /*All the swiperefreshlayout stuff was a feature request by Brian Snipes. It makes it so
        when you pull down on the screen, it refreshes the page. It's a great feature and definitely
        should have been added. */

        mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (mySwipeRefreshLayout.isRefreshing()) {
                            mySwipeRefreshLayout.setRefreshing(false);
                        }
                    }
                },2000);
            }
        });

        /*Javascript is used in many modern websites, but you have to also
        enable DOM storage or the hamburger menu won't work */
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        
        loadWebSite();

        /*This is the second portion to restore state after orientation change.
        The first part is way below and an explanation is given there. I also had to put a
        configchanges line in AndroidManifest.xml. Without it, none of this will work.*/

        if(savedInstanceState==null){
            webView.post(new Runnable() {
                @Override
                public void run() {
                    loadWebSite();
                }
            });
        }

        /*The first is needed to keep each click from opening in your browser,
        but the second is needed to handle the embedded video correctly */
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new ChromeClient());

    }

    //Code to make android back button act as browser back button
    @Override
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    /*This whole class is just to allow you to fullscreen videos. Without this and
    changing setWebChromeClient above to new ChromeClient instead of WebChromeClient,
    you will not be able to fullscreen videos. */
    private class ChromeClient extends WebChromeClient {
        private View mCustomView;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;
        protected FrameLayout mFullscreenContainer;
        private int mOriginalOrientation;
        private int mOriginalSystemUiVisibility;

        ChromeClient() {}

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

        public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
        {
            if (this.mCustomView != null)
            {
                onHideCustomView();
                return;
            }
            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);
        }
    }

    /*But, if you just run the code as is, you'll find out that every time you switch from portrait
    to landscape, the video will reset and you will have to start over. That's obviously not what we
    want, so you're going to have to save and reload the website's state rather than a fresh new version
    of the website on each orientation change*/

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState )
    {
        super.onSaveInstanceState(outState);
        webView.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        webView.restoreState(savedInstanceState);
    }

    private void loadWebSite() {
        webView.loadUrl("https://frontpagelinux.com");
    }

}

0 个答案:

没有答案