Last active
July 14, 2021 15:33
-
-
Save wcoder/f6855c579f6d77c45494439a7f2951db to your computer and use it in GitHub Desktop.
Xamarin Android DotsIndicator View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<DroidDotIndicator.DotsIndicator | |
android:id="@+id/indicator" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Android.Content; | |
using Android.Graphics; | |
using Android.Graphics.Drawables; | |
using Android.Runtime; | |
using Android.Util; | |
using Android.Widget; | |
namespace DroidDotIndicator | |
{ | |
[Register("DroidDotIndicator.DotsIndicator")] | |
public class DotsIndicator : LinearLayout | |
{ | |
private int _pagesCount; | |
private int _selectedPage = -1; | |
#region def ctors | |
public DotsIndicator(Context context) : | |
base(context) | |
{ | |
Initialize(); | |
} | |
public DotsIndicator(Context context, IAttributeSet attrs) : | |
base(context, attrs) | |
{ | |
Initialize(); | |
} | |
public DotsIndicator(Context context, IAttributeSet attrs, int defStyle) : | |
base(context, attrs, defStyle) | |
{ | |
Initialize(); | |
} | |
#endregion | |
void Initialize() | |
{ | |
} | |
public int IndicatorSize { get; set; } = 40; | |
public int IndicatorColor { get; set; } = Color.Transparent; | |
public Color IndicatorBorderColor { get; set; } = Color.Black; | |
public int IndicatorSelectedColor { get; set; } = Color.Red; | |
public int PagesCount | |
{ | |
set | |
{ | |
_pagesCount = value; | |
RemoveAllViews(); | |
SetupPagerIndidcatorDots(); | |
} | |
} | |
public int SelectedPage | |
{ | |
set | |
{ | |
if (value < 0 || value > _pagesCount - 1) | |
{ | |
throw new ArgumentException($"Incorrect index: {value}, max: {_pagesCount - 1}", nameof(value)); | |
} | |
if (_selectedPage >= 0) | |
{ | |
SelectIndicator(false); | |
} | |
_selectedPage = value; | |
SelectIndicator(true); | |
} | |
} | |
private void SetupPagerIndidcatorDots() | |
{ | |
for (var i = 0; i < _pagesCount; i++) | |
{ | |
var image = new ImageView(Context); | |
image.LayoutParameters = CreateIndicatorLayoutParams(); | |
image.SetImageDrawable(CreateDefaultIndicator()); | |
AddView(image); | |
} | |
} | |
private void SelectIndicator(bool selected) | |
{ | |
var selectedImage = (ImageView)GetChildAt(_selectedPage); | |
var drawable = selected ? CreateSelectedIndicator() : CreateDefaultIndicator(); | |
selectedImage.SetImageDrawable(drawable); | |
} | |
protected virtual LayoutParams CreateIndicatorLayoutParams() | |
{ | |
var layoutParams = new LayoutParams(IndicatorSize, IndicatorSize); | |
layoutParams.SetMargins(5, 0, 5, 0); | |
return layoutParams; | |
} | |
protected virtual Drawable CreateDefaultIndicator() | |
{ | |
var drawable = CreateBaseIndicator(); | |
drawable.SetColor(IndicatorColor); | |
return drawable; | |
} | |
protected virtual Drawable CreateSelectedIndicator() | |
{ | |
var drawable = CreateBaseIndicator(); | |
drawable.SetColor(IndicatorSelectedColor); | |
return drawable; | |
} | |
protected virtual GradientDrawable CreateBaseIndicator() | |
{ | |
var drawable = new GradientDrawable(); | |
drawable.SetShape(ShapeType.Oval); | |
drawable.SetStroke(1, IndicatorBorderColor); | |
return drawable; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_indicator = FindViewById<DotsIndicator>(Resource.Id.indicator); | |
_indicator.PagesCount = 5; | |
_indicator.SelectedPage = 4; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment