1

Is ViewPager available for monodroid developers? I've seen a lot of tutorials like this one for Android: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html

I can do this and declare a ViewPager as code, for example:

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

        }
    }
}

But in the tutorial he uses xml code such as this:

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

Which Monodroid doesnt seem to like.. Any input of what I'm missing?

3
  • How do you get the ViewPager class? what library are you using?
    – Matthew
    Mar 9, 2012 at 10:09
  • using Android.Support.V4.Widget Mar 9, 2012 at 10:23
  • The question is, how are you getting this class from C#/Mono? Is there a wrapper that you are using, as there is no C# version of the Android.Support namespace
    – Matthew
    Mar 9, 2012 at 11:33

2 Answers 2

4

Not a direct answer to your question...

But someone's ported a separate ViewPager to MonoDroid: https://github.com/Cheesebaron/MonoDroid.HorizontalPager

And I've then added a simple page indicator based on 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);
    }
}

I will get a sample project in GitHub one day!

4
  • Although this doesn't answer my question it kinda does anyway. Doesn't seem that Monodroid supports ViewPager yet which leads to solutions like this. I'm also using this one, however it's under apache license. How does that effect my application that is gonna be launched on market? Mar 9, 2012 at 13:38
  • I think Apache is pretty permissive - en.wikipedia.org/wiki/Apache_License
    – Stuart
    Mar 9, 2012 at 15:35
  • I totally 100% recommend CheeseBaron's HorizontalPager. Im currently using MonoCross, which still doesn't support Fragments, and I'm too invested to turn back now. Thanks a ton for this suggestion Stuart (and to CheeseBaron!).
    – samus
    Sep 6, 2012 at 13:28
  • Did you ever get around to posting a sample which uses both the Pager and Indicator?
    – Eric
    Apr 5, 2013 at 13:20
2

Even this is answered already:

Its possible with the Support-packages.

I just implemented it following this Java example.

You can just transfer anything that is written there to C# code, there are only a few things to take care of:

  • also the stuff commented out must be used
  • dont call the Class PagerAdapter
  • when the author wrote super.anything()use base.Anything()
  • when instantiating the fragments, make sure that u write the packagename in all lowercase.

Hope this helps anyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.