如何使用进度条和刷卡刷新相同的片段?

时间:2018-09-04 06:21:10

标签: android android-progressbar swiperefreshlayout

我正在使用ProgressBar(带有计数)加载网站。 互联网断开连接时,我正在使用android.support.v4.widget.SwipeRefreshLayout连接到互联网。 和 我使用的是自定义error.html 页面,而不是默认的“无互联网连接”页面。

在添加滑动刷新布局之前,进度条还不错。添加滑动刷新布局后,进度栏不起作用。 我想连接以上3个项目。(进度条,滑动刷新和自定义错误页面) 因此,我连接了所有代码。但无法正常工作。请您能帮帮我吗? 谢谢你。

web_view.java

public class web_view extends AppCompatActivity {

    SwipeRefreshLayout swipe;
    ProgressBar progressBar;
    WebView webView;
    String url="http://blog.google.com";
    TextView textView;
    //to hide progressbar after loading part 1
    LinearLayout liProgressContainer;
    private String currentUrl;
    private AdView mAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_train);
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        textView = (TextView) findViewById(R.id.tvLoadingPercentage);
        //to hide progressbar after loading part 2
        liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);

        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LoadWeb();
            }
        });
        LoadWeb();
   }

    public void LoadWeb()
    {
        webView = (WebView) findViewById(R.id.webView);
        WebSettings browserSetting = webView.getSettings();
        browserSetting.setJavaScriptEnabled(true);
        webView.loadUrl(url);
        swipe.setRefreshing(true);
        webView.setWebViewClient(new MyWebViewClient()
        {
            public void onReceivedError(WebView view,int errorCode,String description ,String failingUrl )
            {
                webView.loadUrl("file:///android_asset/error.html");
            }
            public void onPageFinished(WebView view,String url)
            {
                swipe.setRefreshing(true);
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                textView.setText(progress + " %");
            }
        });
    }


   //back button function
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }


    private class MyWebViewClient extends WebViewClient {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

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

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            liProgressContainer.setVisibility(View.GONE);

            //hide header part
        }
    }
}

activity_webview.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".web_view">

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

    <LinearLayout
        android:id="@+id/liProgressContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:indeterminate="true"/>

        <TextView
            android:id="@+id/tvLoadingPercentage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF" />

    </LinearLayout>

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

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

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

救救我

1 个答案:

答案 0 :(得分:0)

尝试这个代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/liProgressContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ProgressBar
                    android:id="@+id/progressBar"
                    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFF"
                    android:indeterminate="true" />

                <TextView
                    android:id="@+id/tvLoadingPercentage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFF" />

            </LinearLayout>

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/liProgressContainer">

                <WebView
                    android:id="@+id/webView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </android.support.v4.widget.NestedScrollView>
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />

</RelativeLayout>

用此代码替换您的web_view活动代码

public class web_view extends AppCompatActivity {

    SwipeRefreshLayout swipe;
    ProgressBar progressBar;
    WebView webView;
    String url = "http://blog.google.com";
    TextView textView;
    //to hide progressbar after loading part 1
    LinearLayout liProgressContainer;
    private String currentUrl;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        webView = findViewById(R.id.webView);
        progressBar = findViewById(R.id.progressBar);
        textView = findViewById(R.id.tvLoadingPercentage);
        //to hide progressbar after loading part 2
        liProgressContainer = findViewById(R.id.liProgressContainer);
        swipe = findViewById(R.id.swipe);

        webView.setWebViewClient(new MyWebViewClient());

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                textView.setText(progress + " %");
            }
        });

        WebSettings browserSetting = webView.getSettings();
        browserSetting.setJavaScriptEnabled(true);

        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LoadWeb();
            }
        });

        LoadWeb();
    }

    public void LoadWeb() {
        webView.loadUrl(url);
        swipe.setRefreshing(true);
    }


    //back button function
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }


    private class MyWebViewClient extends WebViewClient {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/error.html");
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

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

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            liProgressContainer.setVisibility(View.GONE);
            swipe.setRefreshing(false);
        }
    }
}