Search code examples
xamarinxamarin.androidxamarin-studio

How to Set auto sliders in pageviewers in xamarin.android


I am trying to put auto sliders in my code for page viewers in xamarin.android and tried may ways to do that but it is not working correctly can you please help me to set up the auto slider to the page viewer i am posting my last tried code which is auto sliding one page and jumping off right away to last page.

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

My fragments related code:

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

anyone Please help me with the code to auto slide pageviewers on set time in xamarin android


Solution

  • I think you will need to invoke your method of changing current item to the RunOnUiThread method to ensure this it will be executed in UI thread, for example:

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

    Tested on Android 6.0 emulator:

    enter image description here