11using System ;
22using Expandable ;
33using Xamarin . Forms ;
4+ using TouchEffect ;
5+ using System . Diagnostics ;
46namespace ExpandableViewSample
57{
68 public class ManyViewsPage : ContentPage
79 {
810 public ManyViewsPage ( )
911 {
10- var mainStack = new StackLayout ( ) ;
12+ var mainStack = new StackLayout
13+ {
14+ Spacing = 15
15+ } ;
1116
1217 for ( int i = 0 ; i < 15 ; ++ i )
1318 {
1419 mainStack . Children . Add ( CreateExpandable ( i ) ) ;
1520 }
1621
17- BackgroundColor = Color . White ;
22+ BackgroundColor = Color . Black ;
1823 Content = new ScrollView
1924 {
2025 Padding = new Thickness ( 20 , Device . RuntimePlatform == Device . iOS ? 20 : 0 , 20 , 0 ) ,
@@ -24,31 +29,24 @@ public ManyViewsPage()
2429
2530 private View CreateExpandable ( int number )
2631 {
27- var secondLabel = new Label
32+ var middleImage = new Image
2833 {
29- VerticalTextAlignment = TextAlignment . Center ,
30- HorizontalTextAlignment = TextAlignment . Center ,
31- FontAttributes = FontAttributes . Italic ,
3234 HeightRequest = 40 ,
33- BackgroundColor = Color . Black ,
34- TextColor = Color . White ,
35- Text = $ "The Label of { number } "
35+ Aspect = Aspect . Fill ,
36+ Source = "grad.jpg"
3637 } ;
3738
38- return new ExpandableView
39+ var arrowImage = new Image
3940 {
40- PrimaryView = new Label
41- {
42- FontSize = 22 ,
43- VerticalTextAlignment = TextAlignment . Center ,
44- HorizontalTextAlignment = TextAlignment . Center ,
45- FontAttributes = FontAttributes . Bold ,
46- HeightRequest = 60 ,
47- BackgroundColor = Color . Black ,
48- TextColor = Color . White ,
49- Text = $ "Click to expand { number } "
50- } ,
41+ HorizontalOptions = LayoutOptions . EndAndExpand ,
42+ Source = "arrow_drop_down.png" ,
43+ HeightRequest = 45 ,
44+ WidthRequest = 45
45+ } ;
5146
47+ var exp = new ExpandableView
48+ {
49+ IsTouchToExpandEnabled = false ,
5250 SecondaryViewTemplate = new DataTemplate ( ( ) => new StackLayout
5351 {
5452 Spacing = 10 ,
@@ -59,31 +57,105 @@ private View CreateExpandable(int number)
5957 CornerRadius = 0 ,
6058 FontAttributes = FontAttributes . Italic ,
6159 HeightRequest = 40 ,
62- BackgroundColor = Color . Black ,
63- TextColor = Color . White ,
64- Text = $ "Increase height of label ",
65- Command = new Command ( ( ) => ChangeHeight ( secondLabel , 20 ) )
60+ BackgroundColor = Color . White ,
61+ TextColor = Color . Black ,
62+ Text = $ "Increase height",
63+ Command = new Command ( ( ) => ChangeHeight ( middleImage , 20 ) )
6664 } ,
67- secondLabel ,
65+ middleImage ,
6866 new Button
6967 {
7068 CornerRadius = 0 ,
7169 FontAttributes = FontAttributes . Italic ,
7270 HeightRequest = 40 ,
73- BackgroundColor = Color . Black ,
74- TextColor = Color . White ,
75- Text = $ "Decrease height of the label ",
76- Command = new Command ( ( ) => ChangeHeight ( secondLabel , - 20 ) )
71+ BackgroundColor = Color . White ,
72+ TextColor = Color . Black ,
73+ Text = $ "Decrease height",
74+ Command = new Command ( ( ) => ChangeHeight ( middleImage , - 20 ) )
7775 }
7876 }
7977 } )
8078 } ;
79+
80+ exp . StatusChanged += ( sender , e ) =>
81+ {
82+ var rotation = 0 ;
83+ switch ( e . Status )
84+ {
85+ case ExpandStatus . Collapsing :
86+ break ;
87+ case ExpandStatus . Expanding :
88+ rotation = 180 ;
89+ break ;
90+ default :
91+ return ;
92+ }
93+ arrowImage . RotateTo ( rotation , 200 , Easing . CubicInOut ) ;
94+ } ;
95+
96+ exp . PrimaryView = new TouchView
97+ {
98+ RegularAnimationEasing = Easing . CubicInOut ,
99+ PressedAnimationEasing = Easing . CubicInOut ,
100+ RegularAnimationDuration = 600 ,
101+ PressedAnimationDuration = 600 ,
102+ RegularBackgroundColor = Color . White ,
103+ RippleCount = - 1 ,
104+ PressedBackgroundColor = Color . LightGray ,
105+ PressedScale = 1.2 ,
106+ Command = new Command ( ( ) => exp . IsExpanded = ! exp . IsExpanded )
107+ } ;
108+
109+ exp . PrimaryView . ChildAdded += ( sender , e ) => ExpandableOnChildAdded ( e . Element ) ;
110+
111+ ( exp . PrimaryView as TouchView ) . Children . Add ( new Frame
112+ {
113+ CornerRadius = 5 ,
114+ Padding = new Thickness ( 10 , 0 ) ,
115+ Content = new StackLayout
116+ {
117+ Orientation = StackOrientation . Horizontal ,
118+ HorizontalOptions = LayoutOptions . FillAndExpand ,
119+ Children =
120+ {
121+ new Label
122+ {
123+ FontSize = 22 ,
124+ VerticalTextAlignment = TextAlignment . Center ,
125+ HorizontalTextAlignment = TextAlignment . Center ,
126+ FontAttributes = FontAttributes . Bold ,
127+ HeightRequest = 60 ,
128+ TextColor = Color . Black ,
129+ Text = $ "Click to expand { number } "
130+ } ,
131+ arrowImage
132+ }
133+ }
134+ } ) ;
135+
136+
137+ ( exp . PrimaryView as TouchView ) . StatusChanged += ( sender , args ) =>
138+ {
139+ if ( args . Status == TouchEffect . Enums . TouchStatus . Canceled )
140+ {
141+ ( exp . PrimaryView as TouchView ) . Command . Execute ( null ) ;
142+ }
143+ } ;
144+
145+ return exp ;
146+ }
147+
148+ protected void ExpandableOnChildAdded ( Element child )
149+ {
150+ AbsoluteLayout . SetLayoutFlags ( child , AbsoluteLayoutFlags . All ) ;
151+ AbsoluteLayout . SetLayoutBounds ( child , new Rectangle ( 0 , 0 , 1 , 1 ) ) ;
152+ base . OnChildAdded ( child ) ;
81153 }
82154
83- private void ChangeHeight ( Label target , double sizeChange )
155+ private void ChangeHeight ( View target , double sizeChange )
84156 {
85157 target . HeightRequest += sizeChange ;
86- ( target . Parent . Parent as ExpandableView ) . ForceUpdateSizeCommand . Execute ( null ) ;
158+ ( target . Parent . Parent as ExpandableView ) . ForceUpdateSize ( ) ;
87159 }
88160 }
89161}
0 commit comments