简体   繁体   中英

How to disable sliding animation in Android TabBar with Xamarin.Forms?

I'm using a TabbedPage , and whenever I select a new Tab, there is a sliding animation. I would like to disable the sliding animation .

There is a Xamarin Sample that does this, without the sliding animation. How can I replicate this?

I'm using a custom ToolBar and a TabBar , and also a FormsAppCompatActivity in my AppActivity.

The Sample provided by Xamarin isn't , so I'm not sure how I can disable these animations.

Thank you

Similar to the Native approach described here . This can be done with a custom renderer:

[assembly: ExportRenderer(typeof(TabsPage), typeof(TabsPageRenderer))]
namespace App.Droid.Renderers
{
    public class TabsPageRenderer : BadgedTabbedPageRenderer
    {
        ViewPager _viewPager;
        TabLayout _tabLayout;

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);

            for (int i = 0; i < ChildCount; i++)
            {
                var v = GetChildAt(i);
                if (v is ViewPager)
                    _viewPager = (ViewPager)v;
                else if (v is TabLayout)
                    _tabLayout = (TabLayout)v;
            }

            _viewPager.SetPageTransformer(true, new NoAnimationPageTransformer());

        }

    }

    public class NoAnimationPageTransformer : Java.Lang.Object, Android.Support.V4.View.ViewPager.IPageTransformer
    {
        public void TransformPage(Android.Views.View view, float position)
        {
            if (position < 0)
            {
                view.ScrollX = (int)((float)(view.Width) * position);
            }
            else if (position > 0)
            {
              view.ScrollX = -(int)((float)(view.Width) * -position);
            }
            else
            {
              view.ScrollX = 0;
            }

        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM