简体   繁体   English

Xamarin Android垂直ViewPager

[英]Xamarin Android Vertical ViewPager

I need a vertical ViewPager in Android using Xamarin and the solution doesn't work. 我需要在Android中使用Xamarin的垂直ViewPager,并且该解决方案无法正常工作。 I searched some examples in java but there's an object developed by the git community that do all the work. 我在Java中搜索了一些示例,但是git社区开发了一个对象,可以完成所有工作。 Unfortunately there isn't in Xamarin. 不幸的是Xamarin中没有。 So, this is my code, it doesn't give me error, but it displays only a black screen. 因此,这是我的代码,它没有给我错误,但仅显示黑屏。 Nothing more. 而已。

I Extended the ViewPager class 我扩展了ViewPager类

public class VerticalViewPager : ViewPager {

    public VerticalViewPager (Context context):base(context) {

        Init ();
    }

    public VerticalViewPager(Context context, IAttributeSet attr):base(context, attr) {

        Init();
    }

    public override bool OnTouchEvent (Android.Views.MotionEvent e) {

        e.SetLocation (e.GetY (), e.GetX ());
        return base.OnTouchEvent (e);
    }

    private void Init()  {

        SetPageTransformer (true, new PagerTransformer());
        OverScrollMode = Android.Views.OverScrollMode.Never;
    }
}

and create my PageTransformer 并创建我的PageTransformer

public class PagerTransformer : Java.Lang.Object, ViewPager.IPageTransformer {

    int pageWidth;
    int pageHeight;
    float yPos;

    public PagerTransformer () {}

    public void TransformPage (View view, float position) {

        pageWidth = view.Width;
        pageHeight = view.Height;

        if (position < -1)  {

            view.Alpha = 0;
        } 
        else if (position <= 1)  {

            view.Alpha = 1;
            // Counteract the default slide transition
            view.TranslationX = (pageWidth * -position);
            //set Y position to swipe in from top
            yPos = position + pageHeight;
            view.TranslationY = yPos;
        } 
        else  {

            // This page is way off-screen to the right.
            view.Alpha = 0;
        }
    }
}

In the MainActivity, onCreate method i set 在MainActivity中,我设置了onCreate方法

page = FindViewById<VerticalViewPager> (Resource.Id.vertical_pager);

and obviously page is a VerticalViewPager object. 显然页面是一个VerticalViewPager对象。

If I use a normale ViewPager, the app works fine. 如果我使用常规的ViewPager,则该应用程序可以正常运行。 Any ideas about the reason of the black screen? 关于黑屏的原因有什么想法吗? Any java code is appreciare as well! 任何Java代码也是赞赏的!

Thanks 谢谢

The reason of black screen is that you set your screen position out of visible bounds. 黑屏的原因是您将屏幕位置设置为超出可见范围。 Your visible bounds are from 0 to page height. 您的可见范围是从0到页面高度。 Try his : "yPos = position * pageHeight;" 试试他的:“ yPos =位置* pageHeight;” instead of "yPos = position + pageHeight;" 而不是“ yPos =位置+ pageHeight;”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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