Skip to content

Instantly share code, notes, and snippets.

@wcoder
Last active July 14, 2021 15:37
Show Gist options
  • Save wcoder/e055ca2c6f798fd78b45e16d7891725d to your computer and use it in GitHub Desktop.
Save wcoder/e055ca2c6f798fd78b45e16d7891725d to your computer and use it in GitHub Desktop.
Basic implementation of ViewPager indicator
public class ViewPagerIndicator : LinearLayout, ViewPager.IOnPageChangeListener
{
private TextView _textView;
private ViewPager _viewPager;
#region Android view dafault constructors
public ViewPagerIndicator(Context context)
: base(context)
{
Init(context);
}
public ViewPagerIndicator(Context context, IAttributeSet attrs)
: base(context, attrs)
{
Init(context);
}
public ViewPagerIndicator(Context context, IAttributeSet attrs, int defStyleAttr)
: base(context, attrs, defStyleAttr)
{
Init(context);
}
public ViewPagerIndicator(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes)
: base(context, attrs, defStyleAttr, defStyleRes)
{
Init(context);
}
#endregion
private void Init(Context context)
{
_textView = new TextView(context);
AddView(_textView);
}
public void SetViewPager(ViewPager viewPager)
{
_viewPager = viewPager;
_viewPager.AddOnPageChangeListener(this);
}
public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
}
public void OnPageScrollStateChanged(int state)
{
}
public void OnPageSelected(int position)
{
_textView.Text = position.ToString();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_viewPager?.RemoveOnPageChangeListener(this);
}
}
@wcoder
Copy link
Author

wcoder commented Aug 29, 2017

viewPagerIndicator.SetViewPager(viewPager);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment