Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ViewPager.SetPageTransformer doesnt work without a small delay first

ChristopherDrososChristopherDrosos GRMember ✭✭
edited November 2016 in Xamarin.Android

Im trying to set a transformer on a Viewpager but it doesn't work always correctly. I have 3 fragments inside a viewpager and on the fragment one which is where i have the problem i use another viewpager as an image slider.

I have this code:

public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            slideshowViewPager = view.FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.StaffPhotoViewPager);
            slideshowViewPager.OffscreenPageLimit = 1;
...

var viewPagerAdapter = new SlideshowPagerAdapter(Activity, application.currentStaffImages);
        slideshowViewPager.Adapter = viewPagerAdapter;
        slideshowViewPager.SetPageTransformer (true, new ZoomOutPageTransformer(slideshowViewPager));
}

with this adapter:

public class SlideshowPagerAdapter : PagerAdapter
    {
        List<Uri> _items = new List<Uri>();
        private readonly Activity _context;

        public SlideshowPagerAdapter (Activity context, List<Uri> items) : base()
        {
            _items = items;
            _context = context;
        }
        //public override float GetPageWidth (int position)
        //{
        //  return 1f;
        //}
        public override int Count 
        {
            get { return _items.Count; }
        }
        public override bool IsViewFromObject(View view, Java.Lang.Object objectValue) 
        {
            return view == ((RelativeLayout) objectValue);
        }
        public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {
            var view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.SlideshowViewPager, container, false);
            var imageView = view.FindViewById<ImageView> (Resource.Id.slideshowStaffImageView);
            Picasso.With(_context)
                   .Load(_items [position].ToString())
                   .Into(imageView);
            container.AddView(view);

            return view;
        }
        public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object objectValue)
        {
            container.RemoveView((View)objectValue);
        }
    }

and im using this transformer:

public abstract class FixedPageTransformer : Java.Lang.Object, ViewPager.IPageTransformer
    {
        ViewPager mPager;
        int mClientWidth, mPaddingLeft;
        public FixedPageTransformer(ViewPager pager)
        {
            this.mPager = pager;

            mClientWidth = mPager.Width - mPager.PaddingLeft - mPager.PaddingRight;
            mPaddingLeft = mPager.PaddingLeft / 2;
        }
        public void TransformPage(View view, float v)
        {
            FixedTransformPage(view, (float)(view.Left - (mPager.ScrollX + mPaddingLeft)) / mClientWidth);
        }

        public abstract void FixedTransformPage(View view, float fixedVal);
    }

public class ZoomOutPageTransformer : FixedPageTransformer
    {

        public ZoomOutPageTransformer(ViewPager yourViewPager):base(yourViewPager)
        {
            //this.mPager = yourViewPager;

        }
        private float MIN_SCALE = 0.85f;
        private float MIN_ALPHA = 0.5f;

        public override void FixedTransformPage(View view, float fixedVal)
        {
            int pageWidth = view.Width;
            int pageHeight = view.Height;

            if (fixedVal < -1)
            { // [-Infinity,-1)
              // This page is way off-screen to the left.
                view.Alpha = 0;

            }
            else if (fixedVal <= 1)
            { // [-1,1]
              // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.Max(MIN_SCALE, 1 - Math.Abs(fixedVal));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (fixedVal < 0)
                {
                    view.TranslationX = (horzMargin - vertMargin / 2);
                }
                else {
                    view.TranslationX = (-horzMargin + vertMargin / 2);
                }

                // Scale the page down (between MIN_SCALE and 1)
                view.ScaleX = (scaleFactor);
                view.ScaleY = (scaleFactor);

                // Fade the page relative to its size.
                view.Alpha = (MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                        (1 - MIN_SCALE) * (1 - MIN_ALPHA));

            }
            else { // (1,+Infinity]
                   // This page is way off-screen to the right.
                view.Alpha = 0;
            }
        }
    }

and if i dont put a small delay before initializing the pagetransformer, the viewpager appears empty, why is this happening? how i can avoid it? i though that using onViewCreated will be enough.

Sign In or Register to comment.