如何在同一个片段中使用进度条和刷卡刷新?

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

我正在使用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>

帮我

android android-progressbar swiperefreshlayout
1个回答
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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.