summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/ConditionalFocusLayout.cs
blob: 6368d02319477f745b5e7a235434a6939f1cf5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Android.Content;
using Android.Views;
using Android.Widget;

namespace Xamarin.Forms.Platform.Android
{
	internal class ConditionalFocusLayout : LinearLayout, global::Android.Views.View.IOnTouchListener
	{
		public ConditionalFocusLayout(Context context) : base(context)
		{
			SetOnTouchListener(this);
		}

		public bool OnTouch(global::Android.Views.View v, MotionEvent e)
		{
			bool allowFocus = v is EditText;
			DescendantFocusability = allowFocus ? DescendantFocusability.AfterDescendants : DescendantFocusability.BlockDescendants;
			return false;
		}

		internal void ApplyTouchListenersToSpecialCells(Cell item)
		{
			DescendantFocusability = DescendantFocusability.BlockDescendants;

			global::Android.Views.View aView = GetChildAt(0);
			(aView as EntryCellView)?.EditText.SetOnTouchListener(this);

			var viewCell = item as ViewCell;
			if (viewCell == null || viewCell?.View == null)
				return;

			IVisualElementRenderer renderer = Platform.GetRenderer(viewCell.View);
			if (renderer?.ViewGroup?.ChildCount != 0)
				(renderer.ViewGroup.GetChildAt(0) as EditText)?.SetOnTouchListener(this);

			foreach (Element descendant in viewCell.View.Descendants())
			{
				var element = descendant as VisualElement;
				if (element == null)
					continue;
				renderer = Platform.GetRenderer(element);
				if (renderer?.ViewGroup?.ChildCount == 0)
					continue;
				(renderer.ViewGroup.GetChildAt(0) as EditText)?.SetOnTouchListener(this);
			}
		}
	}
}