ViewPager是否可用于Monodroid开发人员?我看过很多类似的Android教程:http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

我可以这样做并将一个ViewPager声明为代码,例如:

using Android.Support.V4.Widget;

namespace ViewPagerTest
{
    [Activity(Label = "ViewPager", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            ViewPager pager = new ViewPager( this );

            SetContentView(Resource.Layout.Main);

        }
    }
}


但是在本教程中,他使用了如下的xml代码:

<android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


哪个Monodroid似乎不喜欢..
我缺少什么的任何输入吗?

最佳答案

不是您问题的直接答案...

但是有人将一个单独的ViewPager移植到了MonoDroid:https://github.com/Cheesebaron/MonoDroid.HorizontalPager

然后,我基于http://buildmobile.com/how-to-build-an-android-pager-component/#fbid=TnZmgHdBfhF添加了一个简单的页面指示器

// original credit to:
// https://github.com/brucejcooper/Android-Examples/blob/master/PagingScrollerExample/src/com/eightbitcloud/pagingscroller/PageIndicator.java
public class HorizontalPagerIndicator : View
{
    private HorizontalPager _pager;
    private Paint _textPaint;
    private Paint _inactiveDotPaint;
    private Paint _dotPaint;
    private Paint _dotBackgroundPaint;
    private int _textHeight;
    private int _ascent;
    private int _cellSize;
    private float _displayDensity;

    public HorizontalPagerIndicator(Context context, IAttributeSet attrs)
        : base(context, attrs)
    {
        InitPaints(context);
    }


    public HorizontalPagerIndicator(Context context)
        : base(context)
    {
        InitPaints(context);
    }

    private void InitPaints(Context context)
    {
        _displayDensity = context.Resources.DisplayMetrics.Density;

        _textPaint = new Paint {AntiAlias = true, TextSize = DeviceIndependentToPixels(14), Color = Color.Black};

        _inactiveDotPaint = new Paint { AntiAlias = true, Color = Color.Gray };
        _dotPaint = new Paint {AntiAlias = true, Color = Color.White};
        _dotBackgroundPaint = new Paint { AntiAlias = true, Color = Color.Cyan };

        _ascent = -(int)_textPaint.Ascent();
        _textHeight = (int)(_ascent + _textPaint.Descent());
        _cellSize = DeviceIndependentToPixels(_textHeight + 6);
    }


    public HorizontalPager Pager
    {
        get { return _pager; }
        set  {

            if (_pager != null)
            {
                _pager.ScreenChanged -= PagerOnScreenChanged;
            }
            _pager = value;
            if (_pager != null)
            {
                _pager.ScreenChanged += PagerOnScreenChanged;
            }
            UpdatePageCount();
        }
    }

    private void PagerOnScreenChanged(object sender, EventArgs eventArgs)
    {
        Invalidate();
    }

    public void UpdatePageCount()
    {
        RequestLayout();
        Invalidate();
    }

    private int NumPages
    {
        get
        {
            return _pager == null ? 1 : _pager.ChildCount;
        }
    }

    private int ActivePage
    {
        get
        {
            return _pager == null ? 0 : _pager.CurrentScreen;
        }
    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        SetMeasuredDimension(MeasureWidth(widthMeasureSpec), MeasureHeight(heightMeasureSpec));
    }

    private int MeasureWidth(int measureSpec)
    {
        var result = 0;
        var specMode = MeasureSpec.GetMode(measureSpec);
        var specSize = MeasureSpec.GetSize(measureSpec);

        if (specMode == MeasureSpecMode.Exactly)
        {
            // We were told how big to be
            result = specSize;
        }
        else
        {
            result = NumPages * _cellSize;
            if (specMode == MeasureSpecMode.AtMost)
            {
                // Respect AT_MOST value if that was what is called for by
                // measureSpec
                result = Math.Min(result, specSize);
            }
        }
        return result;
    }

    private int MeasureHeight(int measureSpec)
    {
        var result = 0;
        var specMode = MeasureSpec.GetMode(measureSpec);
        var specSize = MeasureSpec.GetSize(measureSpec);


        if (specMode == MeasureSpecMode.Exactly)
        {
            // We were told how big to be
            result = specSize;
        }
        else
        {
            result = _cellSize;
            if (specMode == MeasureSpecMode.AtMost)
            {
                // Respect AT_MOST value if that was what is called for by
                // measureSpec
                result = Math.Min(result, specSize);
            }
        }
        return result;
    }

    protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);

        var numPages = NumPages;
        var activePageIndex = ActivePage;

        var x = (canvas.Width - numPages * _cellSize)/2;

        //var smallBorder = _cellSize/4;
        //var slightlySmallerBorder = smallBorder - 1;
        var emptyDotSize = _cellSize/4;
        var dotOffset = (_cellSize - emptyDotSize) / 2;
        for (var i = 0; i < numPages; i++, x += _cellSize)
        {
            if (i == activePageIndex)
            {
                //var txt = (i + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture);
                //var bounds = new Rect();
                //_textPaint.GetTextBounds(txt, 0, txt.Length, bounds);
                //var oval = new RectF(x + smallBorder, smallBorder, x + _cellSize - smallBorder, _cellSize - smallBorder);
                var oval = new RectF(x + dotOffset, dotOffset, x + dotOffset + emptyDotSize, dotOffset + emptyDotSize);
                var oval1 = new RectF(x + dotOffset - 1, dotOffset - 1, x + dotOffset + emptyDotSize + 1, dotOffset + emptyDotSize + 1);
                canvas.DrawOval(oval1, _dotBackgroundPaint);
                canvas.DrawOval(oval, _dotPaint);
                //canvas.DrawText(txt, x + (_cellSize - bounds.Width()) / 2, (_cellSize - _textHeight) / 2 + _ascent, _textPaint);
            }
            else
            {
                var oval = new RectF(x + dotOffset, dotOffset, x + dotOffset + emptyDotSize, dotOffset + emptyDotSize);
                canvas.DrawOval(oval, _inactiveDotPaint);
            }
        }
    }

    private int DeviceIndependentToPixels(int dpi)
    {
        return (int)Math.Round((float)dpi * _displayDensity);
    }
}


有一天,我将在GitHub上获得一个示例项目!

关于android - Monodroid ViewPager,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9631838/

10-11 09:13