2012-03-09 61 views
1

ViewPager可供Monodroid開發者使用嗎?我見過很多的教程這樣一個爲Android:http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.htmlMonodroid ViewPager

我能做到這一點,並聲明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的似乎不喜歡.. 我失去了我的任何輸入?

+0

如何獲得ViewPager類?你在用什麼庫? – Matthew 2012-03-09 10:09:22

+0

使用Android.Support.V4.Widget – 2012-03-09 10:23:13

+0

問題是,你是如何從C#/ Mono獲得這個類的?有沒有你使用的包裝,因爲沒有Android.Support命名空間的C#版本 – Matthew 2012-03-09 11:33:38

回答

4

不是直接回答你的問題......

但有人在移植單獨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上得到一個示例項目!

+0

儘管這不能回答我的問題,但它有點不管。似乎並沒有Monodroid支持ViewPager,而這導致了這樣的解決方案。我也使用這個,但它是在Apache許可證下。這對我的應用程序將如何在市場上推出有什麼影響? – 2012-03-09 13:38:44

+0

我認爲Apache是​​相當寬容 - http://en.wikipedia.org/wiki/Apache_License – Stuart 2012-03-09 15:35:10

+0

我完全100%推薦CheeseBaron的Horizo​​ntalPager。我目前正在使用MonoCross,它仍然不支持碎片,而且我現在投入了很大的努力來恢復。非常感謝Stuart的這個建議(以及CheeseBaron!)。 – samosaris 2012-09-06 13:28:28

2

即使這已被回答:

它可能與支持包。

我剛剛在this Java example之後實施。

你可以只轉移是有寫C#代碼東西,只有幾件事情要照顧:

  • 也註釋掉的東西,必須使用
  • 不要調用類PagerAdapter
  • 當作者寫道super.anything()使用base.Anything()
  • 當實例化這些片段時,請確保您以全部小寫寫入包名。

希望這可以幫助任何人。