-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathScrollingView.cs
100 lines (89 loc) · 3.31 KB
/
ScrollingView.cs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Tizen.NUI.BaseComponents;
using Tizen.NUI;
using System;
using System.Collections.Generic;
namespace Scrolling
{
/// <summary>
/// Can only have one child.
/// Needs to intercept focus events for child but still able to focus a child.
/// Stores current child that is focused, on intercepting focus events it focuses and
/// scrolls to that child.
/// </summary>
class ScrollingView : View
{
private int currentItem;
private int itemCount;
private View childView;
private bool scrollingStepsOutdated;
private List<int> scrollingSteps;
public ScrollingView()
{
// LayoutGroup needed as Scrolling View is a dervied View class so will
// not propagate layouting unless given a layout.
var groupingLayout = new LayoutGroup();
Layout = groupingLayout;
WidthSpecification = LayoutParamPolicies.MatchParent;
HeightSpecification = LayoutParamPolicies.WrapContent;
Name = "scrollingView";
AddedToWindow += OnWindow;
}
public void OnWindow(object source, EventArgs e)
{
Console.WriteLine("OnWindow ");
}
public override void Add( View child )
{
if(childView)
{
Remove(childView);
itemCount = 0;
childView = null;
}
Console.WriteLine("Adding child "+ child.Name + " to ScrollingView");
childView = child;
base.Add( childView );
itemCount = (int)childView.ChildCount; // elements added to child after this will not be counted.
scrollingStepsOutdated = true;
}
public int Scroll( bool reverse )
{
// Get child's item positions
// Only retrieve from the child if the layout/View dirty otherwise use cached positions.
if ( scrollingStepsOutdated )
{
UpdateScrollingSteps();
}
// Scroll to the position of the required item.
if ( reverse )
{
currentItem-= 1;
}
else
{
currentItem++;
}
currentItem = Math.Min( Math.Max( currentItem, 0 ), itemCount-1 );
Animation scrollAnimation = new Animation();
scrollAnimation.SetDuration(1.0f);
float childViewPositionX = scrollingSteps[currentItem] - scrollingSteps[(childView.ChildCount>1)?1:0];
scrollAnimation.AnimateTo(childView, "PositionX", childViewPositionX);
scrollAnimation.Play();
return currentItem;
}
// Get position of each child in the container that will be scrolled.
// These positions will be the scrolling steps, scrolling with jump to
// these steps.
private void UpdateScrollingSteps()
{
scrollingSteps = new List<int>();
Console.WriteLine("UpdateScrollingSteps child count:"+ childView.ChildCount);
for( uint i = 0; i < childView.ChildCount; ++i )
{
var item = childView.GetChildAt( i );
scrollingSteps.Add( (int)Math.Floor( item.PositionX) );
}
scrollingStepsOutdated = false;
}
}
}