如何在Xamarin.Android中实现带点滑块的ViewPagers?

如何在Xamarin.Android中实现带点滑块的ViewPagers?,xamarin.android,Xamarin.android,我正在学习Xamarin.Android最近,在过去几天的实践中,我试图用点滑块实现PageViewer,但未能实现。我参考了stackoverflow和github等流行集线器的许多解决方案,但未能实现。 有谁能告诉我如何在xamarin.android中实现带点滑块的viewpagers吗。 我附上一张照片给你参考,这将给你一个关于我的要求清晰的看法 正如你所看到的,他正在使用点滑块滑动页面,我正试图这样做。 请向我解释如何在Xamarin.Android(本机)看看这个示例项目“Andro

我正在学习Xamarin.Android最近,在过去几天的实践中,我试图用点滑块实现PageViewer,但未能实现。我参考了stackoverflow和github等流行集线器的许多解决方案,但未能实现。 有谁能告诉我如何在xamarin.android中实现带点滑块的viewpagers吗。 我附上一张照片给你参考,这将给你一个关于我的要求清晰的看法

正如你所看到的,他正在使用点滑块滑动页面,我正试图这样做。
请向我解释如何在Xamarin.Android(本机)

看看这个示例项目“Android ViewPagerIndicator to Xamarin.Android”。您需要包括库,其中有多个实现来显示viewpager上的指示器


首先定义您自己的自定义圆圈页面指示器,将其设置为CirclePageIndicator.cs将其添加到项目中(而不是活动文件) 使用Android.Content

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.Graphics;
使用Android.Support.V4.View;
使用Android.Util;
使用Java.Lang;
使用Java.Interop;
命名空间MyApplication.Droid.Library
{
公共类CirclePageIndicator:视图,页面指示器
{
常量int水平=0;
垂直常数=1;
私人浮动汇率;
私人涂料;
私人油漆;
私人涂料;
私有视图寻呼机mViewPager;
private ViewPager.IOnPageChangeListener mListener;
私有int mCurrentPage;
私家车;
私人国际货币基金组织;
私有int-state;
私人机构的参与;
私人托管;
私人布尔·麦肯特;
私人bool-mSnap;
私有常量int无效_指针=-1;
姆图什洛普私人酒店;
私有浮点mLastMotionX=-1;
private int mActivePointerId=无效的\u指针;
私家车拖拉;
公共CirclePageIndicator(上下文):此(上下文,null)
{
}
public CirclePageIndicator(上下文上下文,IAttributeSet属性):此(上下文,属性,Resource.Attribute.vpiCirclePageIndicatorStyle)
{
}
public CirclePageIndicator(上下文上下文,IAttributeSet属性,int defStyle):基(上下文,属性,defStyle)
{
//从资源加载默认值
var-res=资源;
int defaultPageColor=res.GetColor(Resource.Color.default\u circle\u indicator\u page\u Color);
int defaultFillColor=res.GetColor(Resource.Color.default\u circle\u indicator\u fill\u Color);
int defaultOrientation=res.GetInteger(Resource.Integer.default\u circle\u indicator\u orientation);
int defaultStrokeColor=res.GetColor(Resource.Color.default\u circle\u indicator\u stroke\u Color);
float defaultStrokeWidth=res.GetDimension(Resource.Dimension.default\u circle\u indicator\u stroke\u width);
float defaultRadius=res.GetDimension(Resource.Dimension.default\u circle\u indicator\u radius);
bool defaultCentered=res.GetBoolean(Resource.Boolean.default\u circle\u indicator\u centered);
bool defaultSnap=res.GetBoolean(Resource.Boolean.default\u circle\u indicator\u snap);
//检索样式属性
var a=context.actainstyledattributes(attrs、Resource.Styleable.CirclePageIndicator、defStyle、Resource.Style.Widget\u CirclePageIndicator);
mCentered=a.GetBoolean(Resource.Styleable.CirclePageIndicator_居中,defaultCentered);
mOrientation=a.GetInt(Resource.Styleable.CirclePageIndicator\u方向,默认方向);
mPaintPageFill=新油漆(PaintFlags.AntiAlias);
mPaintPageFill.SetStyle(Paint.Style.Fill);
mPaintPageFill.Color=a.GetColor(Resource.Styleable.CirclePageIndicator\u pageColor,defaultPageColor);
mPaintStroke=新油漆(PaintFlags.AntiAlias);
mpainstroke.SetStyle(Paint.Style.Stroke);
mpainstroke.Color=a.GetColor(Resource.Styleable.CirclePageIndicator\u strokeColor,defaultStrokeColor);
mpainstroke.StrokeWidth=a.GetDimension(Resource.Styleable.CirclePageIndicator_StrokeWidth,defaultStrokeWidth);
mPaintFill=新油漆(PaintFlags.AntiAlias);
mPaintFill.SetStyle(Paint.Style.Fill);
mPaintFill.Color=a.GetColor(Resource.Styleable.CirclePageIndicator\u fillColor,defaultFillColor);
mRadius=a.GetDimension(Resource.Styleable.CirclePageIndicator_半径,defaultRadius);
mSnap=a.GetBoolean(Resource.Styleable.CirclePageIndicator_snap,defaultSnap);
a、 回收();
var configuration=ViewConfiguration.Get(上下文);
mTouchSlop=ViewConfigurationCompat.getscaledpagingtouchlop(配置);
}
公共空间以集合为中心(以布尔为中心)
{
mcenter=居中;
使无效();
}
公共图书馆
{
返回mcenter;
}
公共void SetPageColor(颜色pageColor)
{
mPaintPageFill.Color=pageColor;
使无效();
}
public int GetPageColor()
{
返回mPaintPageFill.Color;
}
公共void SetFillColor(颜色fillColor)
{
mPaintFill.Color=fillColor;
使无效();
}
公共int GetFillColor()
{
返回mPaintFill.Color;
}
公共方向(整数方向)
{
开关(方向)
{
横向案例:
垂直箱
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using System;
using System.Collections.Generic;

namespace myProject.CustomWidgets
{
    public class PageControl : LinearLayout
    {
        #region Properties

        private int _currentPage = 0;

        public int CurrentPage
        {
            get
            {
                return _currentPage;
            }

            set
            {
                _currentPage = value;

                SetCurrentPageIndicator();
            }
        }

        public int Pages = 0;

        public Color PageIndicatorTintColor = ColorHelper.Clear;

        public Color CurrentPageIndicatorTintColor = ColorHelper.Clear;

        private readonly Context context;

        private List<ImageView> ivList = new List<ImageView>();

        private List<Drawable> drawableList = new List<Drawable>();

        private readonly int circleSize = 7;

        #endregion Properties

        #region Constructor

        protected PageControl(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public PageControl(Context context) : base(context)
        {
            this.context = context;
            InitConfig();
        }

        public PageControl(Context context, IAttributeSet attrs) : base(context, attrs)
        {
            this.context = context;
            InitConfig();
        }

        public PageControl(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
        {
            this.context = context;
            InitConfig();
        }

        public PageControl(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
        {
            this.context = context;
            InitConfig();
        }

        #endregion Constructor

        #region Methods

        private void InitConfig()
        {
            Orientation = Orientation.Horizontal;
        }

        protected override void OnAttachedToWindow()
        {
            base.OnAttachedToWindow();

            Render();
        }

        private void Render()
        {
            // Start with a clean slate
            RemoveAllViews();
            ivList = new List<ImageView>();
            drawableList = new List<Drawable>();

            if (Pages <= 0)
            {
                return;
            }

            for (int i = 0; i < Pages; i++)
            {
                var iv = new ImageView(context);

                var size = ConvertionHelper.DensityToPixels(context, circleSize);
                var margin = (int)(size / 2.5);

                var lp = new LayoutParams(size, size);
                lp.SetMargins(margin, 0, margin, 0);

                iv.LayoutParameters = lp;

                /* By default, all drawables instances loaded from the same resource share a common state.
                 * If you modify the state of one instance, all the other instances will receive the same modification.
                 * Calling this method on a mutable Drawable will have no effect */
                var drawable = ResourcesHelper.GetDrawable(context, Resource.Drawable.ic_circle_separator).Mutate();
                drawable.SetColorFilter(PageIndicatorTintColor, PorterDuff.Mode.SrcAtop);

                iv.SetImageDrawable(drawable);

                drawableList.Add(drawable);

                ivList.Add(iv);

                AddView(iv);
            }

            // Initial current page indicator set-up
            SetCurrentPageIndicator();
        }

        private void SetCurrentPageIndicator()
        {
            if (ivList.Count == 0 || drawableList.Count == 0 || ivList.Count != drawableList.Count ||
                CurrentPage < 0 || CurrentPage >= ivList.Count)
            {
                return;
            }

            // Reset all colors
            for (int i = 0; i < ivList.Count; i++)
            {
                drawableList[i].SetColorFilter(PageIndicatorTintColor, PorterDuff.Mode.SrcIn);
                ivList[i].SetImageDrawable(drawableList[i]);
            }

            // Change color of current page indicator
            drawableList[CurrentPage].SetColorFilter(CurrentPageIndicatorTintColor, PorterDuff.Mode.SrcIn);
            ivList[CurrentPage].SetImageDrawable(drawableList[CurrentPage]);
        }

        #endregion Methods
    }
}
<myProject.PageControl
        android:id="@+id/pagecontrol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>
var pagecontrol = FindViewById<PageControl>(Resource.Id.pagecontrol);
pagecontrol.PageIndicatorTintColor = ColorHelper.PositiveBlue;
pagecontrol.CurrentPageIndicatorTintColor = ColorHelper.Orange;
pagecontrol.CurrentPage = 0;
pagecontrol.Pages = 3;