Changed code to avoid the usage of some APIs that require an specific API Level on Android (#9175)

This commit is contained in:
Javier Suárez Ruiz 2020-02-01 03:15:35 +01:00 коммит произвёл GitHub
Родитель ca7a557cfb
Коммит 348bba2a01
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 66 добавлений и 37 удалений

Просмотреть файл

@ -16,7 +16,6 @@ using AButton = Android.Support.V7.Widget.AppCompatButton;
using Android.Support.V7.Widget; using Android.Support.V7.Widget;
#endif #endif
using Android.Views; using Android.Views;
using Android.Widget;
using Xamarin.Forms.Internals; using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific; using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using APointF = Android.Graphics.PointF; using APointF = Android.Graphics.PointF;
@ -32,10 +31,11 @@ namespace Xamarin.Forms.Platform.Android
const int SwipeThresholdMargin = 0; const int SwipeThresholdMargin = 0;
const int SwipeItemWidth = 100; const int SwipeItemWidth = 100;
const long SwipeAnimationDuration = 200; const long SwipeAnimationDuration = 200;
const double SwipeMinimumDelta = 10;
readonly Context _context; readonly Context _context;
GestureDetector _detector; GestureDetector _detector;
AView _scrollParent; View _scrollParent;
AView _contentView; AView _contentView;
LinearLayoutCompat _actionView; LinearLayoutCompat _actionView;
SwipeTransitionMode _swipeTransitionMode; SwipeTransitionMode _swipeTransitionMode;
@ -48,6 +48,8 @@ namespace Xamarin.Forms.Platform.Android
SwipeDirection? _swipeDirection; SwipeDirection? _swipeDirection;
float _swipeOffset; float _swipeOffset;
float _swipeThreshold; float _swipeThreshold;
double _previousScrollX;
double _previousScrollY;
bool _isDisposed; bool _isDisposed;
public SwipeViewRenderer(Context context) : base(context) public SwipeViewRenderer(Context context) : base(context)
@ -56,7 +58,8 @@ namespace Xamarin.Forms.Platform.Android
_context = context; _context = context;
AutoPackage = false; AutoPackage = false;
ClipToOutline = true;
this.SetClipToOutline(true, Element);
} }
protected override void OnElementChanged(ElementChangedEventArgs<SwipeView> e) protected override void OnElementChanged(ElementChangedEventArgs<SwipeView> e)
@ -147,29 +150,29 @@ namespace Xamarin.Forms.Platform.Android
{ {
base.OnAttachedToWindow(); base.OnAttachedToWindow();
if (Forms.IsLollipopOrNewer && Control != null) if (Element != null && _scrollParent == null)
{ {
_scrollParent = Parent.GetParentOfType<NestedScrollView>(); _scrollParent = Element.FindParentOfType<ScrollView>();
if (_scrollParent != null) if (_scrollParent is ScrollView scrollView)
{ {
_scrollParent.ScrollChange += OnParentScrollChange; scrollView.Scrolled += OnParentScrolled;
return; return;
} }
_scrollParent = Parent.GetParentOfType<AbsListView>(); _scrollParent = Element.FindParentOfType<ListView>();
if (_scrollParent is AbsListView listView) if (_scrollParent is ListView listView)
{ {
listView.ScrollStateChanged += OnParentScrollStateChanged; listView.Scrolled += OnParentScrolled;
return; return;
} }
_scrollParent = Parent.GetParentOfType<RecyclerView>(); _scrollParent = Element.FindParentOfType<Xamarin.Forms.CollectionView>();
if (_scrollParent != null) if (_scrollParent is Xamarin.Forms.CollectionView collectionView)
{ {
_scrollParent.ScrollChange += OnParentScrollChange; collectionView.Scrolled += OnParentScrolled;
} }
} }
} }
@ -194,12 +197,14 @@ namespace Xamarin.Forms.Platform.Android
if (_scrollParent != null) if (_scrollParent != null)
{ {
if (_scrollParent is AbsListView listView) if (_scrollParent is ScrollView scrollView)
listView.ScrollStateChanged += OnParentScrollStateChanged; scrollView.Scrolled -= OnParentScrolled;
else
_scrollParent.ScrollChange -= OnParentScrollChange;
_scrollParent = null; if (_scrollParent is ListView listView)
listView.Scrolled -= OnParentScrolled;
if (_scrollParent is Xamarin.Forms.CollectionView collectionView)
collectionView.Scrolled -= OnParentScrolled;
} }
if (_contentView != null) if (_contentView != null)
@ -230,7 +235,7 @@ namespace Xamarin.Forms.Platform.Android
float x = Math.Abs((_downX - e.GetX()) / density); float x = Math.Abs((_downX - e.GetX()) / density);
float y = Math.Abs((_downY - e.GetY()) / density); float y = Math.Abs((_downY - e.GetY()) / density);
if (e.Action != MotionEventActions.Move | (x > 10f || y > 10f)) if (e.Action != MotionEventActions.Move | (x > SwipeMinimumDelta || y > SwipeMinimumDelta))
{ {
_detector.OnTouchEvent(e); _detector.OnTouchEvent(e);
} }
@ -1078,28 +1083,21 @@ namespace Xamarin.Forms.Platform.Android
ResetSwipe(); ResetSwipe();
} }
void OnParentScrollChange(object sender, ScrollChangeEventArgs e) void OnParentScrolled(object sender, ScrolledEventArgs e)
{ {
if (sender is RecyclerView recyclerView) var horizontalDelta = e.ScrollX - _previousScrollX;
{ var verticalDelta = e.ScrollY - _previousScrollY;
var scrollState = (ScrollState)recyclerView.ScrollState;
if (scrollState == ScrollState.Fling || scrollState == ScrollState.TouchScroll) if (horizontalDelta > SwipeMinimumDelta || verticalDelta > SwipeMinimumDelta)
ResetSwipe(); ResetSwipe();
}
else
{
var x = Math.Abs(e.ScrollX - e.OldScrollX);
var y = Math.Abs(e.ScrollY - e.OldScrollY);
if (x > 10 || y > 10) _previousScrollX = e.ScrollX;
ResetSwipe(); _previousScrollY = e.ScrollY;
}
} }
void OnParentScrollStateChanged(object sender, AbsListView.ScrollStateChangedEventArgs e) void OnParentScrolled(object sender, ItemsViewScrolledEventArgs e)
{ {
if (e.ScrollState == ScrollState.Fling || e.ScrollState == ScrollState.TouchScroll) if (e.HorizontalDelta > SwipeMinimumDelta || e.VerticalDelta > SwipeMinimumDelta)
ResetSwipe(); ResetSwipe();
} }

Просмотреть файл

@ -11,6 +11,8 @@ using Android.Views;
using AView = Android.Views.View; using AView = Android.Views.View;
using AColor = Android.Graphics.Color; using AColor = Android.Graphics.Color;
using Android.Graphics; using Android.Graphics;
using System.Collections.Generic;
using System.Linq;
namespace Xamarin.Forms.Platform.Android namespace Xamarin.Forms.Platform.Android
{ {
@ -192,5 +194,24 @@ namespace Xamarin.Forms.Platform.Android
return view.Parent.GetParentOfType<T>(); return view.Parent.GetParentOfType<T>();
} }
internal static T FindParentOfType<T>(this VisualElement element)
{
var navPage = element.GetParentsPath()
.OfType<T>()
.FirstOrDefault();
return navPage;
}
internal static IEnumerable<Element> GetParentsPath(this VisualElement self)
{
Element current = self;
while (!Application.IsApplicationOrNull(current.RealParent))
{
current = current.RealParent;
yield return current;
}
}
} }
} }

Просмотреть файл

@ -17,6 +17,7 @@ namespace Xamarin.Forms.Platform.iOS
const int SwipeThresholdMargin = 0; const int SwipeThresholdMargin = 0;
const double SwipeItemWidth = 100; const double SwipeItemWidth = 100;
const double SwipeAnimationDuration = 0.2; const double SwipeAnimationDuration = 0.2;
const double SwipeMinimumDelta = 10;
View _scrollParent; View _scrollParent;
UIView _contentView; UIView _contentView;
@ -31,6 +32,8 @@ namespace Xamarin.Forms.Platform.iOS
double _swipeThreshold; double _swipeThreshold;
CGRect _originalBounds; CGRect _originalBounds;
List<CGRect> _swipeItemsRect; List<CGRect> _swipeItemsRect;
double _previousScrollX;
double _previousScrollY;
bool _isDisposed; bool _isDisposed;
public SwipeViewRenderer() public SwipeViewRenderer()
@ -1031,12 +1034,19 @@ namespace Xamarin.Forms.Platform.iOS
void OnParentScrolled(object sender, ScrolledEventArgs e) void OnParentScrolled(object sender, ScrolledEventArgs e)
{ {
ResetSwipe(); var horizontalDelta = e.ScrollX - _previousScrollX;
var verticalDelta = e.ScrollY - _previousScrollY;
if (horizontalDelta > SwipeMinimumDelta || verticalDelta > SwipeMinimumDelta)
ResetSwipe();
_previousScrollX = e.ScrollX;
_previousScrollY = e.ScrollY;
} }
void OnParentScrolled(object sender, ItemsViewScrolledEventArgs e) void OnParentScrolled(object sender, ItemsViewScrolledEventArgs e)
{ {
if (e.HorizontalDelta > 10 || e.VerticalDelta > 10) if (e.HorizontalDelta > SwipeMinimumDelta || e.VerticalDelta > SwipeMinimumDelta)
ResetSwipe(); ResetSwipe();
} }