Last active
April 21, 2022 20:57
-
-
Save mecvillarina/60e45f4e1d8919b8c916de2b37d415f2 to your computer and use it in GitHub Desktop.
Xamarin Forms ScrollView - Change ScrollBarColor using RoutingEffect
This file contains 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 UIEffects = ScrollView.UI.Effects; | |
[assembly: ResolutionGroupName(Constants.EffectsNamespace)] | |
[assembly: ExportEffect(typeof(ScrollBarColorEffect), nameof(ScrollBarColorEffect))] | |
namespace ScrollView.Droid.Effects | |
{ | |
public class ScrollBarColorEffect : PlatformEffect | |
{ | |
protected override void OnAttached() | |
{ | |
UpdateUI(); | |
} | |
protected override void OnDetached() | |
{ | |
} | |
void UpdateUI() | |
{ | |
try | |
{ | |
Java.Lang.Reflect.Field mScrollCacheField = Class.FromType(typeof(Android.Views.View)).GetDeclaredField("mScrollCache"); | |
mScrollCacheField.Accessible = true; | |
var mScrollCache = mScrollCacheField.Get(Control); | |
var scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar"); | |
scrollBarField.Accessible = true; | |
var scrollBar = scrollBarField.Get(mScrollCache); | |
var method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Class.FromType(typeof(Drawable))); | |
method.Accessible = true; | |
var layers = new Drawable[1]; | |
var shapeDrawable = new ShapeDrawable(new RectShape()); | |
var scrollBarColor = Color.Default; | |
var effect = (UIEffects.ScrollBarColorEffect)Element.Effects.FirstOrDefault(e => e is UIEffects.ScrollBarColorEffect); | |
if (effect != null) | |
{ | |
scrollBarColor = effect.ScrollBarColor; | |
} | |
shapeDrawable.Paint.Color = scrollBarColor.ToAndroid(); | |
shapeDrawable.SetIntrinsicWidth(5); | |
layers[0] = shapeDrawable; | |
method.Invoke(scrollBar, layers); | |
} | |
catch | |
{ | |
} | |
} | |
} | |
} |
This file contains 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 UIEffects = ScrollView.UI.Effects; | |
[assembly: ResolutionGroupName(Constants.EffectsNamespace)] | |
[assembly: ExportEffect(typeof(ScrollBarColorEffect), nameof(ScrollBarColorEffect))] | |
namespace ScrollView.iOS.Effects | |
{ | |
public class ScrollBarColorEffect : PlatformEffect | |
{ | |
UIScrollView _view; | |
UIColor _scrollBarColor; | |
protected override void OnAttached() | |
{ | |
Initialize(); | |
} | |
protected override void OnDetached() | |
{ | |
Uninitialize(); | |
} | |
void Initialize() | |
{ | |
_view = (UIScrollView)(Control ?? Container); | |
_view.Scrolled += Container_Scrolled; | |
var effect = (UIEffects.ScrollBarColorEffect)Element.Effects.FirstOrDefault(e => e is UIEffects.ScrollBarColorEffect); | |
if(effect != null) | |
{ | |
_scrollBarColor = effect.ScrollBarColor.ToUIColor(); | |
} | |
} | |
void Uninitialize() | |
{ | |
_view.Scrolled -= Container_Scrolled; | |
} | |
private void Container_Scrolled(object sender, System.EventArgs e) | |
{ | |
var subViews = _view.Subviews.ToList(); | |
var verticalIndicator = subViews.LastOrDefault(); | |
if(verticalIndicator != null) | |
{ | |
verticalIndicator.BackgroundColor = _scrollBarColor; | |
} | |
} | |
} | |
} |
This file contains 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
namespace ScrollView.UI.Effects | |
{ | |
public class ScrollBarColorEffect : RoutingEffect | |
{ | |
public ScrollBarColorEffect() : base($"{Constants.EffectsNamespace}.{nameof(ScrollBarColorEffect)}") | |
{ | |
} | |
public Color ScrollBarColor { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment