From 9dff4c16508452512c5faa36788f65ba6419170e Mon Sep 17 00:00:00 2001 From: adrianknight89 Date: Tue, 6 Dec 2016 06:07:29 -0600 Subject: [Android] ScrollView should send correct ScrollX and ScrollY (#394) * Android should show correct ScrollX and ScrollY when scrolling in both directions * Adding sample code to demonstrate scrolling * Orientation fix * ScrollTo should work for horizontal + vertical scrolling * Get correct scroll x and y values for ScrollOrientation.Both * Convert positions to pixels * Adding unit test to watch out for incorrect animation positioning * automated test * improvements * fixed texts --- .../Renderers/ScrollViewRenderer.cs | 49 +++++++++++++++++----- 1 file changed, 39 insertions(+), 10 deletions(-) (limited to 'Xamarin.Forms.Platform.Android/Renderers/ScrollViewRenderer.cs') diff --git a/Xamarin.Forms.Platform.Android/Renderers/ScrollViewRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ScrollViewRenderer.cs index 2f66d8e8..6db51c23 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ScrollViewRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ScrollViewRenderer.cs @@ -202,7 +202,18 @@ namespace Xamarin.Forms.Platform.Android internal void UpdateScrollPosition(double x, double y) { if (_view != null) + { + if (_view.Orientation == ScrollOrientation.Both) + { + if (x == 0) + x = Forms.Context.FromPixels(_hScrollView.ScrollX); + + if (y == 0) + y = Forms.Context.FromPixels(ScrollY); + } + Controller.SetScrolledPosition(x, y); + } } static int GetDistance(double start, double position, double v) @@ -248,8 +259,8 @@ namespace Xamarin.Forms.Platform.Android var x = (int)Forms.Context.ToPixels(e.ScrollX); var y = (int)Forms.Context.ToPixels(e.ScrollY); - int currentX = _view.Orientation == ScrollOrientation.Horizontal ? _hScrollView.ScrollX : ScrollX; - int currentY = _view.Orientation == ScrollOrientation.Horizontal ? _hScrollView.ScrollY : ScrollY; + int currentX = _view.Orientation == ScrollOrientation.Horizontal || _view.Orientation == ScrollOrientation.Both ? _hScrollView.ScrollX : ScrollX; + int currentY = _view.Orientation == ScrollOrientation.Vertical || _view.Orientation == ScrollOrientation.Both ? ScrollY : _hScrollView.ScrollY; if (e.Mode == ScrollToMode.Element) { Point itemPosition = Controller.GetScrollPositionForElement(e.Element as VisualElement, e.Position); @@ -275,10 +286,19 @@ namespace Xamarin.Forms.Platform.Android return; } - if (_view.Orientation == ScrollOrientation.Horizontal) - _hScrollView.ScrollTo(distX, distY); - else - ScrollTo(distX, distY); + switch (_view.Orientation) + { + case ScrollOrientation.Horizontal: + _hScrollView.ScrollTo(distX, distY); + break; + case ScrollOrientation.Vertical: + ScrollTo(distX, distY); + break; + default: + _hScrollView.ScrollTo(distX, distY); + ScrollTo(distX, distY); + break; + } }; animator.AnimationEnd += delegate { @@ -291,10 +311,19 @@ namespace Xamarin.Forms.Platform.Android } else { - if (_view.Orientation == ScrollOrientation.Horizontal) - _hScrollView.ScrollTo(x, y); - else - ScrollTo(x, y); + switch (_view.Orientation) + { + case ScrollOrientation.Horizontal: + _hScrollView.ScrollTo(x, y); + break; + case ScrollOrientation.Vertical: + ScrollTo(x, y); + break; + default: + _hScrollView.ScrollTo(x, y); + ScrollTo(x, y); + break; + } Controller.SendScrollFinished(); } } -- cgit v1.2.3