根据自己写的Java版本的pagerview滑动指示器改写的xamarin版本,原理解析参见文章http://blog.csdn.net/u013835855/article/details/71159888,好了,其它不多说,直接上源码:
using System; using Android.Content; using Android.Views; using Android.Widget; using Android.Util; using Android.Graphics; using Android.Support.V4.View; namespace FirstApp.Widget { class MyIndicator : HorizontalScrollView, ViewPager.IOnPageChangeListener, View.IOnClickListener { private static uint COLOR_TEXT_NORMAL = 0xFF000000; private static int COLOR_INDICATOR_COLOR = Android.Graphics.Color.Black; private Context context; private int tabWidth; private string[] titles; private int count; private Paint mPaint; private float mTranslationX; private ViewPager viewPager; private int SCREEN_WIDTH; public float lineheight = 2.0f; public MyIndicator(Context context) : base(context) { init(context); } public MyIndicator(Context context, IAttributeSet attribute) : base(context, attribute) { init(context); } public MyIndicator(Context context, IAttributeSet attribute, int defStyleAttr) : base(context, attribute, defStyleAttr) { init(context); } public void init(Context context) { this.context = context; mPaint = new Paint(); mPaint.SetARGB(255, 0, 0, 0); mPaint.StrokeWidth = lineheight; HorizontalScrollBarEnabled = false; SCREEN_WIDTH = context.Resources.DisplayMetrics.WidthPixels; } public void setTitles(string[] titles) { this.titles = titles; count = titles.Length; tabWidth = SCREEN_WIDTH / 4; generateTitleView(); } public void setPager(ViewPager viewPager) { this.viewPager = viewPager; viewPager.AddOnPageChangeListener(this); } public void setLineHeight(float height) { this.lineheight = height; mPaint.StrokeWidth = height;//底部指示线的宽度 } public override void Draw(Canvas canvas) { base.Draw(canvas); canvas.Save(); canvas.Translate(mTranslationX, Height - lineheight); canvas.DrawLine(0, 0, tabWidth, 0, mPaint);//(startX, startY, stopX, stopY, paint) canvas.Restore(); } public void scroll(int position, float offset) { mTranslationX = tabWidth * (position + offset); ScrollTo((int)mTranslationX - (SCREEN_WIDTH - tabWidth) / 2, 0); Invalidate(); } private void generateTitleView() { if (ChildCount > 0) this.RemoveAllViews(); count = titles.Length; LinearLayout linearLayout = new LinearLayout(context); linearLayout.Orientation = Orientation.Horizontal; linearLayout.LayoutParameters = new LinearLayout.LayoutParams(count * tabWidth, LinearLayout.LayoutParams.MatchParent); for (int i = 0; i < count; i++) { TextView tv = new TextView(context); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(tabWidth, LinearLayout.LayoutParams.MatchParent); tv.Gravity = GravityFlags.Center; tv.SetTextColor(new Android.Graphics.Color(0, 0, 0)); tv.Text = titles[i]; tv.TextSize = 16;//字体大小 tv.LayoutParameters = lp; tv.SetTag(Resource.String.key, i); tv.SetOnClickListener(this); linearLayout.AddView(tv); } AddView(linearLayout); } public void OnPageScrollStateChanged(int state) { } public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels) { scroll(position, positionOffset); } public void OnPageSelected(int position) { } public void OnClick(Android.Views.View v) { if (viewPager != null) { viewPager.SetCurrentItem((int)v.GetTag(Resource.String.key), true); } } } } 具体使用如下: indicor = (MyIndicator)FindViewById(Resource.Id.indicor); titles = new string[] {"a", "b", "c", "d", "e", "f", "g" }; indicor.setTitles(titles); viewPager = (ViewPager)FindViewById(Resource.Id.viewPager1); MyPagerAdapter adapter = new MyPagerAdapter(this, titles); viewPager.Adapter = adapter; indicor.setPager(viewPager); 有问题留言!