2017-02-27 145 views
0

我想在Xamarin.android的webview中實現SwipeRefreshLayout,但佈局不斷加載無限作爲一個進度條,但刷卡時無法正常工作。請幫我修復它。刷新刷新不工作在xamarin.android webview

protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     SetContentView(Resource.Layout.gameofthrones); 

     myWebClient = new WebClient(); 







     myWebView = FindViewById<WebView>(Resource.Id.webView); 
     myProgressBar = FindViewById<ProgressBar>(Resource.Id.progressBar); 
     myswipeRefreshLayout = FindViewById<SwipeRefreshLayout>(Resource.Id.refresher); 
     myswipeRefreshLayout.SetColorScheme(Resource.Color.Red, Resource.Color.Orange, 
              Resource.Color.Yellow, Resource.Color.Green, 
              Resource.Color.Blue, Resource.Color.Indigo, 
              Resource.Color.Violet); 
     var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar); 

     myWebView.Settings.JavaScriptEnabled = true; 
     myWebView.LoadUrl("http://www.google.com"); 
     myWebView.SetWebViewClient(myWebClient); 



     //Toolbar will now take on default actionbar characteristics 
     SetSupportActionBar(toolbar); 

     SupportActionBar.Title = "GAME OF THRONES"; 

     SupportActionBar.SetDisplayHomeAsUpEnabled(true); 
     SupportActionBar.SetHomeButtonEnabled(true); 
     myswipeRefreshLayout.Refresh += MyswipeRefreshLayout_Refresh; 


    } 

    private void MyswipeRefreshLayout_Refresh(object sender, EventArgs e) 
    { 

     myWebClient.myOnProgressChanged += (int state) => 
     { 
      if (state == 0) 
      { 
       (sender as SwipeRefreshLayout).Refreshing = false; 
       myswipeRefreshLayout.Refreshing = false; 

       //page loaded no progress bar visible 
       //myProgressBar.Visibility = ViewStates.Invisible; 
      } 
      else 
      { 
       (sender as SwipeRefreshLayout).Refreshing = true; 
       myswipeRefreshLayout.Refreshing = true; 
       //myProgressBar.Visibility = ViewStates.Visible; 
      } 
     }; 
    } 

    public override bool OnKeyDown(Android.Views.Keycode keyCode, Android.Views.KeyEvent e) 
    { 
     if (keyCode == Keycode.Back && myWebView.CanGoBack()) 
     { 
      myWebView.GoBack(); 
      return true; 
     } 

     return base.OnKeyDown(keyCode, e); 
    } 

    public override bool OnOptionsItemSelected(IMenuItem item) 
    { 
     if (item.ItemId == Android.Resource.Id.Home) 
      Finish(); 

     return base.OnOptionsItemSelected(item); 
    } 
} 
    public class WebClient : WebViewClient 
    { 
     public delegate void ToggleProgreeBar(int state); 
     public ToggleProgreeBar myOnProgressChanged; 

     public override bool ShouldOverrideUrlLoading(WebView view, string url) 
     { 
      view.LoadUrl(url); 
      return true; 
     } 

     public override void OnPageStarted(WebView view, string url, Bitmap favicon) 
     { 
      if (myOnProgressChanged != null) 
      { 
       myOnProgressChanged.Invoke(1); 
      } 
      base.OnPageStarted(view, url, favicon); 
     } 
     public override void OnPageFinished(WebView view, string url) 
     { 
      if (myOnProgressChanged != null) 
      { 
       myOnProgressChanged.Invoke(0); 
      } 
      base.OnPageFinished(view, url); 
     } 

回答

0

SwipeRefreshLayout.Refresh當用戶向下滑動時被觸發。根據您的要求,當用戶刷卡,則需要手動刷新網頁視圖:

private void SwipeRefresh_Refresh(object sender, System.EventArgs e) 
{ 
     webview.LoadUrl(webview.Url); 
} 

和寄存器內OnCreatemyWebClient.myOnProgressChanged事件。因此,代碼應如下所示:

public class MainActivity : Activity 
{ 
    WebView webview; 
    SwipeRefreshLayout mySwipeRefresh; 
    WebClient myWebClient; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 
     webview = FindViewById<WebView>(Resource.Id.webview); 
     webview.Settings.JavaScriptEnabled = true; 
     webview.LoadUrl("http://www.google.com"); 
     mySwipeRefresh = FindViewById<SwipeRefreshLayout>(Resource.Id.swiperefresh); 
     myWebClient = new WebClient(); 
     //register myOnProgressChanged here 
     myWebClient.myOnProgressChanged += (int state) => 
     { 
      if (state == 0) 
      { 
       mySwipeRefresh.Refreshing = false; 

      } 
      else 
      { 
       mySwipeRefresh.Refreshing = true; 
      } 
     }; 
     webview.SetWebViewClient(myWebClient); 
     mySwipeRefresh.Refresh += SwipeRefresh_Refresh; 
    } 

    private void SwipeRefresh_Refresh(object sender, System.EventArgs e) 
    { 
     //refresh the webview when user swipes. 
     webview.LoadUrl(webview.Url); 
    } 
} 
+0

感謝埃爾維斯爲您提供快速和正確的答案。它工作正常。請問你能告訴我如何處理,當設備沒有互聯網連接,並顯示可能是一個敬酒而不是默認沒有互聯網的Android頁面? –

+0

我強烈建議你爲這個問題做一個新帖子,以便每個人都可以通過谷歌找到。 –