Last active
October 6, 2016 02:37
-
-
Save winnicki/52ba025047f5be46a8a4f6c4c4fc7c4c to your computer and use it in GitHub Desktop.
An enhanced version of Android's ViewPager.PageScrolled event using Reactive Extensions.
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
Observable.FromEventPattern<ViewPager.PageScrolledEventArgs> ( | |
x => _viewPager.PageScrolled += x, | |
x => _viewPager.PageScrolled -= x) | |
.Select (args => args.EventArgs) | |
.StartWith(new ViewPager.PageScrolledEventArgs(0, 0, 0)) | |
.PairWithPrevious() | |
.Skip(1) | |
.Select(args => { | |
var scrollDirection = | |
args.Item1.Position != args.Item2.Position | |
? ScrollDirection.Center | |
: args.Item1.PositionOffset > args.Item2.PositionOffset | |
? ScrollDirection.Left | |
: ScrollDirection.Right; | |
return new { | |
Current = _pagerAdapter.GetItem(args.Item2.Position) as Walkthrough, | |
Next = _pagerAdapter.GetItem(args.Item2.Position+1) as Walkthrough, | |
ScrollDirection = scrollDirection, | |
args.Item2.PositionOffset, | |
}; | |
}) | |
.Where(args => args.ScrollDirection == ScrollDirection.Center || args.Current != null && args.Next != null) | |
.Subscribe (args => { | |
if (args.ScrollDirection == ScrollDirection.Center) { | |
args.Current.View?.SetBackgroundColor (args.Current.ViewModel.BackgroundColor.ToNative()); | |
} else if (args.ScrollDirection == ScrollDirection.Left || args.ScrollDirection == ScrollDirection.Right) { | |
var color = args.Current.ViewModel.BackgroundColor.Lerp (args.Next.ViewModel.BackgroundColor, args.PositionOffset).ToNative (); | |
args.Current.View?.SetBackgroundColor (color); | |
args.Next.View?.SetBackgroundColor (color); | |
} | |
}).DisposeWith(ControlBindings); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment