如何在Webview中停止滑动刷新进度条?

问题描述 投票:1回答:1

我是Android开发的新手。使用SwipeToRefresh中的WebView选项时遇到麻烦。我正在尝试从mounth上解决它,并且尝试了几个代码之后,它不起作用。而且Youtube video不能从WebView全屏播放...这是我的问题:如何隐藏SwipeToRefresh ProgressBar并停止运行?

这是我的MainActivity类:

    public class WebViewActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {

        private WebView myWebView;
        private SwipeRefreshLayout mSwipeRefreshLayout;

        //HTML5 video
        private View mCustomView;
        private int mOriginalSystemUiVisibility;
        private WebChromeClient.CustomViewCallback mCustomViewCallback;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            OneSignal.startInit(this)
                    .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
                    .init();
            setContentView(R.layout.activity_web_view);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            MobileAds.initialize(getApplicationContext(), "ca-app-pub-App Id");
            AdView mAdView = (AdView) findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

            //WebView
            mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshlayout);
            myWebView = (WebView) findViewById(R.id.webview);
            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                    myWebViewSettings();

            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            //improve webView performance
            myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            myWebView.getSettings().setAppCacheEnabled(true);
            myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webSettings.setDomStorageEnabled(true);
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            webSettings.setUseWideViewPort(true);
            webSettings.setSavePassword(true);
            webSettings.setSaveFormData(true);
            webSettings.setEnableSmoothTransition(true);

            myWebView.loadUrl("http://www.youtube.com");
            //force links open in webview only
                    myWebView.reload(); // refreshes the WebView
                }
            });

            myWebView.setWebViewClient(new MyWebViewClient());
        }



        @SuppressLint("SetJavaScriptEnabled")
        @SuppressWarnings("deprecation")
        private void myWebViewSettings() {
            // set javascript and zoom and some other settings
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setBuiltInZoomControls(true);
            myWebView.getSettings().setDisplayZoomControls(false);
            myWebView.getSettings().setAppCacheEnabled(true);
            myWebView.getSettings().setDatabaseEnabled(true);
            myWebView.getSettings().setDomStorageEnabled(true);
            myWebView.getSettings().setUseWideViewPort(true);
            myWebView.getSettings().setLoadWithOverviewMode(true);

            // enable all plugins (flash)
            myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        }

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.web_view, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.share:
                    Intent shareIntent = new Intent(Intent.ACTION_SEND);
                    shareIntent.setType("text/plain");
                    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                    shareIntent.putExtra(Intent.EXTRA_TEXT, "Subject more   ");
                    startActivity(Intent.createChooser(shareIntent, "Share Via"));
                    break;
                default:
                    break;

            }
            return true;
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();



            if (id == R.id.facebook) {
                // Handle the camera action
                myWebView.loadUrl("http://www.facebook.com");
            } else if (id == R.id.google) {
                myWebView.loadUrl("http://www.google.com");

            }
            //share in navigation
            else if (id == R.id.share) {

                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Androidwebview");
                shareIntent.putExtra(Intent.EXTRA_TEXT, "Try out this cool app www.applink.com");
                startActivity(Intent.createChooser(shareIntent, "Share Via"));

            }
            //rate us in navigation
            else if (id == R.id.rateus) {

                String str = getPackageName();
                try
                {
                    startActivity(new Intent("android.intent.action.VIEW", Uri.parse("market://details?id=" + str)));
                }
                catch (ActivityNotFoundException localActivityNotFoundException)
                {
                    startActivity(new Intent("android.intent.action.VIEW", Uri.parse("https://play.google.com/store/apps/details?id=" + str)));
                }
            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
        private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
            // show error if no network

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);

                view.loadUrl("file:///android_asset/error.png");
            }
            // load from
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
            }
            //progress bar
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                super.onPageStarted(view, url, favicon);
                findViewById(R.id.progress1).setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                findViewById(R.id.progress1).setVisibility(View.GONE);
            }
        }

        //goto previous page when pressing back button

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        if (myWebView.canGoBack()) {
                            myWebView.goBack();
                        } else {
                            finish();
                        }
                        return true;
                }
            }
            return super.onKeyDown(keyCode, event);
        }
        // This fires when a notification is opened by tapping on it or one is received while the app is running.
        private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
            @Override
            public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
                try {
                    if (additionalData != null) {
                        if (additionalData.has("actionSelected"))
                            Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");

                        Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
    }

这里是XML文件:

    <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"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/content_web_view"
        android:layout_width="fill_parent"
        android:layout_above="@+id/adView"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.androidwebview.WebViewActivity"
        tools:showIn="@layout/app_bar_web_view">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/refreshlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webview"
            android:layout_above="@+id/adView"
            android:visible="false"
            android:layout_alignParentLeft="true" />``

        </android.support.v4.widget.SwipeRefreshLayout>


        <FrameLayout
            android:id="@+id/customViewContainer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone"/>


        <ProgressBar
            android:id="@+id/progress1"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_width="80dp"
            android:layout_height="80dp" />


        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/banner_ad_unit_id">
        </com.google.android.gms.ads.AdView>

    </RelativeLayout>
android swiperefreshlayout
1个回答
0
投票

我有同样的问题,但我在另一个线程中发现了这个问题:

final SwipeRefreshLayout swipeLayout = 
   (SwipeRefreshLayout)this.findViewById(R.id.swipeToRefresh);
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myWebView.reload(); // refreshes the WebView

                if (null != swipeLayout) {
                    swipeLayout.setRefreshing(false);
                }
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.