Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Library/Interfaces/IIconGravityProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Android.Views;

namespace DK.Ostebaronen.Droid.ViewPagerIndicator.Interfaces
{
public interface IIconGravityProvider
{
GravityFlags IconGravity { get; }
}
}

1 change: 1 addition & 0 deletions Library/Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="ViewPagerIndicatorEventHandlers.cs" />
<Compile Include="TitlePageIndicator.cs" />
<Compile Include="UnderlinePageIndicator.cs" />
<Compile Include="Interfaces\IIconGravityProvider.cs" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\color\vpi__dark_theme.xml" />
Expand Down
28 changes: 22 additions & 6 deletions Library/TabPageIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected override void OnDetachedFromWindow()
RemoveCallbacks(_tabSelector);
}

private void AddTab(int index, ICharSequence text, int iconResId)
private void AddTab(int index, ICharSequence text, int iconResId, GravityFlags iconGravity)
{
var tabView = new TabView(Context, this) {Focusable = true, Index = index, TextFormatted = text};
tabView.Click += (sender, args) =>
Expand All @@ -113,7 +113,23 @@ private void AddTab(int index, ICharSequence text, int iconResId)
};

if (iconResId != 0)
tabView.SetCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
{
switch (iconGravity) {
case GravityFlags.Top:
tabView.SetCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
break;
case GravityFlags.Right:
tabView.SetCompoundDrawablesWithIntrinsicBounds(0, 0, iconResId, 0);
break;
case GravityFlags.Bottom:
tabView.SetCompoundDrawablesWithIntrinsicBounds(0, 0, 0, iconResId);
break;
case GravityFlags.Left:
default:
tabView.SetCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
break;
}
}

_tabLayout.AddView(tabView, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MatchParent, 1));
}
Expand Down Expand Up @@ -178,9 +194,9 @@ public void NotifyDataSetChanged()
{
_tabLayout.RemoveAllViews();
var adapter = _viewPager.Adapter;
IIconPageAdapter iconAdapter = null;
if (adapter is IIconPageAdapter)
iconAdapter = (IIconPageAdapter)adapter;
var iconAdapter = adapter as IIconPageAdapter;
var gravityProvider = adapter as IIconGravityProvider;
var iconGravity = gravityProvider != null ? gravityProvider.IconGravity : GravityFlags.NoGravity;

var count = adapter.Count;
for(var i = 0; i < count; i++)
Expand All @@ -190,7 +206,7 @@ public void NotifyDataSetChanged()
var iconResId = 0;
if (iconAdapter != null)
iconResId = iconAdapter.GetIconResId(i);
AddTab(i, title, iconResId);
AddTab(i, title, iconResId, iconGravity);
}
if (_selectedTabIndex > count)
_selectedTabIndex = count - 1;
Expand Down
10 changes: 9 additions & 1 deletion Sample/Tabs/SampleTabsWithIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DK.Ostebaronen.Droid.ViewPagerIndicator.Interfaces;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
using Android.Views;

namespace Sample.Tabs
{
Expand All @@ -27,7 +28,9 @@ protected override void OnCreate(Bundle savedInstanceState)
_indicator.SetViewPager(_pager);
}

private class GoogleMusicAdapter : TestFragmentAdapter, IIconPageAdapter
private class GoogleMusicAdapter : TestFragmentAdapter, IIconPageAdapter
// TODO uncomment to see IIconGravityProvider effect (the icon renders on top of the tab title)
//, IIconGravityProvider
{
private static readonly string[] Content =
{
Expand Down Expand Up @@ -56,6 +59,11 @@ public override int Count
public override Fragment GetItem(int p0) { return TestFragment.NewInstance(Content[p0 % Content.Length]); }

public override Java.Lang.ICharSequence GetPageTitleFormatted(int p0) { return new Java.Lang.String(Content[p0 % Content.Length].ToUpper()); }

public GravityFlags IconGravity
{
get { return GravityFlags.Top; }
}
}
}
}