将加载微调器添加到webview Android应用程序

时间:2017-02-14 03:33:40

标签: android progress-bar

我正在尝试添加加载微调器以在网页加载时显示。

这是我的代码:

package com.wEgyptianpost;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.TextView;

import java.util.Random;

import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.AdView;

import com.wEgyptianpost.R;


public class MainActivity extends Activity {
    WebView view;
    SwipeRefreshLayout mySwipeRefreshLayout;

    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AdView adView = (AdView) findViewById(R.id.admob_id);

        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();

        adView.loadAd(adRequest);



        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);
        final WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
        swipeRefreshLayout.setColorSchemeResources(R.color.refresh,R.color.refresh1,R.color.refresh2);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mWebView.reload();
                swipeRefreshLayout.setRefreshing(true);
                (new Handler()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mWebView.stopLoading();
                        swipeRefreshLayout.setRefreshing(false);





                    }
                },10000);
            }
        });
        // Force links and redirects to open in the WebView instead of in a browser
        //mWebView.setWebViewClient(new WebViewClient());

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        // Use remote resource
         mWebView.loadUrl("http:google.com");

        // Stop local links and redirects from opening in browser instead of WebView
         mWebView.setWebViewClient(new MyAppWebViewClient());

        // Use local resource
        //mWebView.loadUrl("file:android_asset/web/google.html");


    }



    // Prevent the back-button from closing the app
    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } 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.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

这是我的活动xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">


    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


        <ScrollView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

   </ScrollView>


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

    <com.google.android.gms.ads.AdView android:id="@+id/admob_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-1851250777225639/8300259410"
        android:layout_gravity="center_horizontal|bottom" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar2"
        android:layout_gravity="center" />


</FrameLayout>

我在活动xml中添加了进度条,但我不知道如何将其链接到主活动代码。

3 个答案:

答案 0 :(得分:0)

你可以试试这个,

 @EnableScheduling
 @SpringBootApplication
 public class Scheduler{
     // doCallScheduleJob Code
 }

class ScheduleJob{

  @Scheduled(cron="${cron.expression.job1}")
  public sycName1(){
           ///doSomething()     
  }

  @Scheduled(cron="${cron.expression.job2}")
  public sycName2(){
           ///doSomething()     
  }

  @Scheduled(cron="${cron.expression.job3}")
  public sycName3(){
           ///doSomething()     
  }

答案 1 :(得分:0)

webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar2.setProgress(newProgress);

                //newProgress maybe is 1~100


                super.onProgressChanged(view, newProgress);
            }

答案 2 :(得分:0)

您可以通过设置webclient来添加它 对于 SET

 mWebView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

        progressBar.setVisibility(View.VISIBLE);
    }
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
    view.loadUrl(url);
    return true;
}
    @Override
    public void onPageFinished(final WebView view, String url) {

                progressBar.setVisibility(View.GONE);
            }
        } ); 

要避免Ajax calls

在活动中调用此

MyClient client=new MyClient();
mWebView.setWebViewClient(client);
client.setFirsttime();

并在此活动中创建一个类

public void MyClient extends WebViewClient(){
private boolean oneTime=false;
     public void setFirsttime(){
         oneTime=true;
       }
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if(oneTime){
                 progressBar.setVisibility(View.VISIBLE);
                   onetime=false;
                 }

        }
           @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
               view.loadUrl(url);
               return true;
               }
        @Override
        public void onPageFinished(final WebView view, String url) {

                    progressBar.setVisibility(View.GONE);
                }
            } 

在多次调用Ajax调用onPageStarted时,progressBar将不会被隐藏。

注意:每次使用client.setFirsttime();时都必须致电mWebView.loadUrl();