【问题标题】:How to swipe WebViews in Xamarin Android如何在 Xamarin Android 中滑动 WebView
【发布时间】:2014-06-29 16:53:28
【问题描述】:

我是 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#。欢迎提出任何建议。提前致谢。

【问题讨论】:

  • 每次触摸都会加载所有 3 个页面,因此最后一个页面在触摸后始终可见

标签: android webview xamarin


【解决方案1】:
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;
                }
            }
        }
    }
}

【讨论】:

  • 您好,我尝试了代码,但这对我不起作用。虽然我对其进行了一些修改,但它仍然具有与我相同的行为。我不明白数组成员 Count;我认为这是变量计数。
猜你喜欢
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多