Search code examples
androidwebviewswipe-gesture

Swipe to Refresh hides before loading the WebView


I implemented swipe to refresh for my WebView and its working fine. But there is one problem which i am not able to solve. The problem is my Swipe to Refresh hides after 6 seconds. It did not remain there till the loading for WebView is completed. What i want is that Swipe to refresh should remain visible till the page fully loads.

My Implementations

swipeView = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
myWebView = (WebView) view.findViewById(R.id.webview);
myWebView.loadUrl("http://m.facebook.com");
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()    
        {
        @Override
         public void onRefresh() 
         {
                     swipeView.setRefreshing(true);
                   ( new Handler()).postDelayed(new Runnable() 
                           {
                             @Override
                             public void run() 
                               {
                                swipeView.setRefreshing(false);
                                myWebView.loadUrl("http://m.facebook.com");
                               }
                         }, 6000);
                   }});

Solution

  • Use WebView listener and finish your Swipe in the onPageFinished listener..

    For example like below

    mWebView.setWebViewClient(new WebViewClient() {
    
       public void onPageFinished(WebView view, String url) {
            // do your stuff here
    
          swipeView.setRefreshing(false);
        }
    });
    

    For your case, change your code like below,

    swipeView = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
    myWebView = (WebView) view.findViewById(R.id.webview);
    mWebView.setWebViewClient(new WebViewClient() {
    
       public void onPageFinished(WebView view, String url) {
            // do your stuff here
    
          swipeView.setRefreshing(false);
        }
    });
    swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()    
            {
            @Override
             public void onRefresh() 
             {
                   myWebView.loadUrl("http://m.facebook.com");
    
             }});