滑动刷新在xamarin.android webview中无效

时间:2017-02-27 15:22:59

标签: c# android visual-studio xamarin.android

我正在尝试在Xamarin.android中的webview中实现SwipeRefreshLayout,但布局保持无限加载仅适用于进度条,但是当刷卡刷新它不起作用时。请帮我解决。

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);
        }

1 个答案:

答案 0 :(得分:0)

用户向下滑动时会触发

SwipeRefreshLayout.Refresh。根据您的要求,当用户滑动时,您需要手动刷新webview:

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

myWebClient.myOnProgressChanged内注册OnCreate个事件。因此代码应如下所示:

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);
    }
}