如何在Xamarin Android中滑动WebViews

时间:2014-06-29 16:53:29

标签: android webview xamarin

我是Xamarin Android的新手。我正在尝试实现一个程序,通过在WebView底部滑动手指来滑动多个WebView。到目前为止,我已在主要活动中编写了以下代码:

using System;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Webkit;

namespace WebSwipe
{
    [Activity(Label = "WebSwipe", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        private WebView _webview;
        private ImageView touchband;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            _webview = FindViewById<WebView>(Resource.Id.webview);
            touchband = FindViewById<ImageView>(Resource.Id.imageView1);
            _webview.Settings.JavaScriptEnabled = true;
            _webview.LoadUrl("http://www.gnu.org");
            touchband.Touch += OnTouch;
        }

        private void OnTouch(object sender, View.TouchEventArgs touchEventArgs)
        {
            string[] pages = new string[] {
            "http://www.computerchess.org.uk",
            "http://gcc.gnu.org",
            "http://es.wikipedia.org"
                             };

            for (int count = 0; count < 3; ++count) 
            {
                switch (touchEventArgs.Event.Action & MotionEventActions.Mask) 
                {
                    case MotionEventActions.Down:
                    case MotionEventActions.Move:
                    case MotionEventActions.Up:
                        _webview.LoadUrl (pages [count]);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}

我知道此代码不起作用,因为通过滑动第一页“http://www.gnu.org”,“我被发送到最后一页”http://es.wikipedia.org“,而不通过中间页面。我仍然无法弄清楚如何实现这一点;请原谅我的无知,但我来自C ++背景,我几乎没有学习C#。欢迎提出任何建议。提前致谢。

1 个答案:

答案 0 :(得分:0)

using System;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Webkit;

namespace WebSwipe
{
    [Activity(Label = "WebSwipe", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        private WebView _webview;
        private ImageView touchband;

        string[] pages;
        int currentPage = 0;

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

            pages = new string[] {
                "http://www.gnu.org"
                "http://www.computerchess.org.uk",
                "http://gcc.gnu.org",
                "http://es.wikipedia.org"
                 };

            SetContentView(Resource.Layout.Main);
            _webview = FindViewById<WebView>(Resource.Id.webview);
            touchband = FindViewById<ImageView>(Resource.Id.imageView1);
            _webview.Settings.JavaScriptEnabled = true;
            _webview.LoadUrl(pages[currentPage]);
            touchband.Touch += OnTouch; 
        }

        private void OnTouch(object sender, View.TouchEventArgs touchEventArgs)
        {

            for (int count = 0; count < 3; ++count) 
            {
                switch (touchEventArgs.Event.Action & MotionEventActions.Mask) 
                {
                    case MotionEventActions.Down:
                    case MotionEventActions.Move:
                    case MotionEventActions.Up:
                        currentPage++;
                        if (currentPage >= pages.Count) currentPage = 0;

                        _webview.LoadUrl (pages [currentPage]);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}