如何在xamarin.android中的网页浏览器中设置自动滑块

时间:2017-04-21 05:18:34

标签: xamarin xamarin.android xamarin-studio

我正在尝试在我的代码中为xamarin.android中的页面查看器添加自动滑块,并尝试了可能的方法,但它无法正常工作,请你帮我设置自动滑块到页面查看器我是发布我最近尝试过的代码,它会自动滑动一页并立即跳到最后一页。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.View;
using Android.Support.V4.App;
using MyApplication.Droid.Library;
using System.Timers;

namespace MyApplication.Droid.Circles
{
    [Activity(Label = "SampleCirclesSnap")]

    public class SampleCirclesSnap : FragmentActivity
    {
       public TestFragmentAdapter mAdapter;
        public ViewPager mPager;
        public PageIndicator mIndicator;
        public Timer time;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.simple_circles);

            mAdapter = new TestFragmentAdapter(SupportFragmentManager);

            mPager = FindViewById<ViewPager>(Resource.Id.pager);
            mPager.Adapter = mAdapter;
            var indicator = FindViewById<CirclePageIndicator>(Resource.Id.indicator);
            mIndicator = indicator;
            indicator.SetViewPager(mPager);
            indicator.SetSnap(true);

            time = new System.Timers.Timer();
            time.Elapsed += (sender, args) => viewPager.SetCurrentItem(CurrentItem++, true);
            time.Interval = 1000;
            time.Enabled = true;
        }
    }
}

我的片段相关代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;

using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;

namespace MyApplication.Droid
{
    public class TestFragmentAdapter : FragmentPagerAdapter
    {
       // public static string[] CONTENT = new string[] { "This", "Is", "A", "Test", };
        public static int[] CONTENT = new int[] { Resource.Drawable.Visa, Resource.Drawable.home_s, Resource.Drawable.Set_s, Resource.Drawable.Icon, Resource.Drawable.home_s };
        int mCount;

        public TestFragmentAdapter(FragmentManager fm) : base(fm)
        {
            mCount = CONTENT.Count();
        }

        public override Fragment GetItem(int position)
        {
            return new TestFragment(CONTENT[position % CONTENT.Count()]);
        }

        public override int Count
        {
            get
            {
                return mCount;
            }
        }

        public void SetCount(int count)
        {
            Console.WriteLine("Setting count to " + count);
            if (count > 0 && count <= 10)
            {
                mCount = count;
                NotifyDataSetChanged();
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Fragment = Android.Support.V4.App.Fragment;

namespace MyApplication.Droid
{
    class TestFragment : Fragment
    {
        private const string KEY_CONTENT = "TestFragment:Content";
        string mContent = "???";
        private int v;

        public TestFragment()
        {
        }

        public TestFragment(int v)
        {
            this.v = v;
        }

        //public TestFragment(string content)
        //{

        //    var builder = new StringBuilder();
        //    for (int i = 0; i < 20; i++)
        //    {
        //        if (i != 19)
        //            builder.Append(content).Append(" ");
        //        else
        //            builder.Append(content);
        //    }
        //    mContent = builder.ToString();

        //}

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            //if ((savedInstanceState != null) && savedInstanceState.ContainsKey(KEY_CONTENT))
            //{
            //    mContent = savedInstanceState.GetString(KEY_CONTENT);
            //}
            ImageView image = new ImageView(Activity);
            image.SetImageResource(v);
            //TextView text = new TextView(Activity);
            //text.Gravity = GravityFlags.Center;
            //text.Text = mContent;
            //text.TextSize = (20 * Resources.DisplayMetrics.Density);
            //text.SetPadding(20, 20, 20, 20);

            LinearLayout layout = new LinearLayout(Activity);
            layout.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
            layout.SetGravity(GravityFlags.Center);
            layout.AddView(image);

            return layout;
        }

        public override void OnSaveInstanceState(Bundle outState)
        {
            base.OnSaveInstanceState(outState);
            outState.PutString(KEY_CONTENT, mContent);
        }
    }
}

任何人请帮我在xamarin android

中设置时间自动滑动网页浏览器的代码

1 个答案:

答案 0 :(得分:1)

我认为您需要调用将当前项更改为RunOnUiThread方法的方法,以确保它将在UI线程中执行,例如:

var timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Enabled = true;
int page = 0;
timer.Elapsed += (sender, args) =>
{
    RunOnUiThread(() =>
    {
        if (page <= viewPager.Adapter.Count)
        {
            page++;
        }
        else
        {
            page = 0;
        }
        viewPager.SetCurrentItem(page, true);
        Log.WriteLine(LogPriority.Debug, "CurrentItem:", viewPager.CurrentItem.ToString());
    });
};

在Android 6.0模拟器上测试:

enter image description here