8000 Create Fsharp.fs · githubxiaoa/AccordionView@3975191 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3975191

Browse files
authored
Create Fsharp.fs
Fsharp version
1 parent dcae287 commit 3975191

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

Accordion/Fsharp.fs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
type DefaultAccordionItemTemplate() as self =
2+
inherit AbsoluteLayout()
3+
4+
do
5+
self.Padding <- new Thickness(5.)
6+
self.HeightRequest <- 50.
7+
let title =
8+
new Label(HorizontalTextAlignment = TextAlignment.Start, HorizontalOptions = LayoutOptions.StartAndExpand)
9+
let price =
10+
new Label(HorizontalTextAlignment = TextAlignment.End, HorizontalOptions = LayoutOptions.End)
11+
12+
title.SetBinding(Label.TextProperty, "Date", stringFormat = "{0:dd MMM yyyy}")
13+
price.SetBinding(Label.TextProperty, "Amount", stringFormat = "{0:C2}")
14+
15+
self.Children.Add(title, new Rectangle(0., 0.5, 0.5, 1.), AbsoluteLayoutFlags.All)
16+
self.Children.Add(price, new Rectangle(1., 0.5, 0.5, 1.), AbsoluteLayoutFlags.All)
17+
18+
type AccordionSectionView(template: DataTemplate, parent: ScrollView) as self =
19+
inherit StackLayout()
20+
21+
let content = new StackLayout(HeightRequest = 0.)
22+
let headerColor = Color.FromHex "0067B7"
23+
let arrowRight = FileImageSource.op_Implicit "ic_keyboard_arrow_right_white_24dp.png"
24+
let arrowDown = FileImageSource.op_Implicit "ic_keyboard_arrow_down_white_24dp.png"
25+
let header = new AbsoluteLayout(BackgroundColor = headerColor)
26+
let headerIcon = new Image(VerticalOptions = LayoutOptions.Center, Source = arrowRight)
27+
let headerTitle = new Label(TextColor = Color.White, VerticalTextAlignment = TextAlignment.Center, HeightRequest = 50., BackgroundColor = headerColor)
28+
29+
do
30+
header.Children.Add(headerIcon, new Rectangle(0., 1., 0.1, 1.), AbsoluteLayoutFlags.All)
31+
header.Children.Add(headerTitle, new Rectangle(1., 1., 0.9, 1.), AbsoluteLayoutFlags.All)
32+
self.Spacing <- 0.
33+
self.Children.Add(header)
34+
self.Children.Add(content)
35+
36+
header.GestureRecognizers.Add(
37+
new TapGestureRecognizer(Command = new Command(fun () ->
38+
if (content.IsVisible && content.HeightRequest > 0.) then
39+
headerIcon.Source <- arrowRight
40+
content.HeightRequest <- 0.
41+
content.IsVisible <- false
42+
else
43+
headerIcon.Source <- arrowDown
44+
content.HeightRequest <- (float)(content.Children.Count * 50)
45+
content.IsVisible <- true
46+
47+
// Scroll top by the current Y position of the section
48+
parent.ScrollToAsync(0., self.Y, true)
49+
|> Async.AwaitTask
50+
|> Async.StartImmediate
51+
)))
52+
53+
member val HeaderTitle = headerTitle
54+
member val Content = content
55+
member val Template = template
56+
57+
static member ChangeTitle bindable oldValue newValue =
58+
if (oldValue <> newValue) then (unbox<AccordionSectionView> bindable).HeaderTitle.Text <- string newValue
59+
10000
60+
static member PopulateList (bindable: BindableObject) oldValue newValue =
61+
if (oldValue <> newValue) then
62+
let view = (unbox<AccordionSectionView> bindable)
63+
view.Content.Children.Clear()
64+
65+
for item in view.ItemsSource do
66+
let template = unbox<View> (view.Template.CreateContent())
67+
template.BindingContext <- item
68+
view.Content.Children.Add(template)
69+
70+
static member ItemsSourceProperty =
71+
BindableProperty.Create(
72+
propertyName = "ItemsSource",
73+
returnType = typeof<IList>,
74+
declaringType = typeof<AccordionSectionView>,
75+
defaultValue = Unchecked.defaultof<IList>,
76+
propertyChanged = new BindableProperty.BindingPropertyChangedDelegate(AccordionSectionView.PopulateList))
77+
78+
member self.ItemsSource
79+
with get (): IList = unbox<IList> <| self.GetValue(AccordionSectionView.ItemsSourceProperty)
80+
and set (value: IList) = self.SetValue(AccordionSectionView.ItemsSourceProperty, value)
81+
82+
static member TitleProperty =
83+
BindableProperty.Create(
84+
propertyName = "Title",
85+
returnType = typeof<string>,
86+
declaringType = typeof<AccordionSectionView>,
87+
propertyChanged = new BindableProperty.BindingPropertyChangedDelegate(AccordionSectionView.ChangeTitle))
88+
89+
member self.Title
90+
with get () = unbox<string> <| self.GetValue(AccordionSectionView.TitleProperty)
91+
and set (value: string) = self.SetValue(AccordionSectionView.TitleProperty, value)
92+
93+
type AccordionView(itemTemplate) as self =
94+
inherit ScrollView()
95+
96+
do self.Content <- new StackLayout(Spacing = 1.)
97+
98+
member val Template = new DataTemplate(fun () -> box (new AccordionSectionView(itemTemplate, self))) with get, set
99+
member val SubTemplate = itemTemplate with get, set
100+
101+
static member PopulateList bindable oldValue newValue =
102+
if (oldValue <> newValue) then
103+
let view = (unbox<AccordionView> bindable)
104+
let content = unbox<Layout<View>> view.Content
105+
content.Children.Clear()
106+
107+
for item in view.ItemsSource do
108+
let template = unbox<View> (view.Template.CreateContent())
109+
template.BindingContext <- item
110+
content.Children.Add(template)
111+
112+
static member ItemsSourceProperty =
113+
BindableProperty.Create(
114+
propertyName = "ItemsSource",
115+
returnType = typeof<IList>,
116+
declaringType = typeof<AccordionSectionView>,
117+
defaultValue = Unchecked.defaultof<IList>,
118+
propertyChanged = new BindableProperty.BindingPropertyChangedDelegate(AccordionView.PopulateList))
119+
120+
member self.ItemsSource
121+
with get (): IList = unbox<IList> <| self.GetValue(AccordionSectionView.ItemsSourceProperty)
122+
and set (value: IList) = self.SetValue(AccordionSectionView.ItemsSourceProperty, value)

0 commit comments

Comments
 (0)
0