@@ -49,6 +49,10 @@ protected override UICollectionViewLayout SelectLayout()
49
49
50
50
var layout = new UICollectionViewCompositionalLayout ( ( sectionIndex , environment ) =>
51
51
{
52
+ if ( VirtualView is null )
53
+ {
54
+ return null ;
55
+ }
52
56
double sectionMargin = 0.0 ;
53
57
if ( ! IsHorizontal )
54
58
{
@@ -77,57 +81,67 @@ protected override UICollectionViewLayout SelectLayout()
77
81
var group = IsHorizontal ? NSCollectionLayoutGroup . GetHorizontalGroup ( groupSize , item , 1 ) :
78
82
NSCollectionLayoutGroup . GetVerticalGroup ( groupSize , item , 1 ) ;
79
83
80
- int currentPosition = 0 ;
81
-
82
84
// Create our section layout
83
85
var section = NSCollectionLayoutSection . Create ( group : group ) ;
84
86
section . InterGroupSpacing = itemSpacing ;
85
87
section . OrthogonalScrollingBehavior = UICollectionLayoutSectionOrthogonalScrollingBehavior . GroupPagingCentered ;
86
88
section . VisibleItemsInvalidationHandler = ( items , offset , env ) =>
87
89
{
90
+ //This will allow us to SetPosition when we are scrolling the items
91
+ //based on the current page
88
92
var page = ( offset . X + sectionMargin ) / env . Container . ContentSize . Width ;
89
93
90
- // Check if we are at the beginning or end of the page
91
- if ( Math . Abs ( page % 1 ) <= ( double . Epsilon * 100 ) && Controller . ItemsSource . ItemCount > 0 )
94
+ // Check if we not are at the beginning or end of the page and if we have items
95
+ if ( Math . Abs ( page % 1 ) > ( double . Epsilon * 100 ) || Controller . ItemsSource . ItemCount <= 0 )
92
96
{
93
- var pageIndex = ( int ) page ;
97
+ return ;
98
+ }
99
+
100
+ var pageIndex = ( int ) page ;
101
+ var carouselPosition = pageIndex ;
102
+
103
+ var cv2Controller = ( CarouselViewController2 ) Controller ;
94
104
95
- if ( ItemsView . Loop )
105
+ //If we are looping, we need to get the correct position
106
+ if ( ItemsView . Loop )
107
+ {
108
+ var maxIndex = ( Controller . ItemsSource as ILoopItemsViewSource ) . LoopCount - 1 ;
109
+
110
+ //To mimic looping, we needed to modify the ItemSource and inserted a new item at the beginning and at the end
111
+ if ( pageIndex == maxIndex )
96
112
{
97
- var maxIndex = ( Controller . ItemsSource as Items . ILoopItemsViewSource ) . LoopCount - 1 ;
113
+ //When at last item, we need to change to 2nd item, so we can scroll right or left
114
+ pageIndex = 1 ;
115
+ }
116
+ else if ( pageIndex == 0 )
117
+ {
118
+ //When at first item, need to change to one before last, so we can scroll right or left
119
+ pageIndex = maxIndex - 1 ;
120
+ }
98
121
99
- //To mimic looping, we needed to modify the ItemSource and inserted a new item at the beginning and at the end
100
- if ( pageIndex == maxIndex )
101
- {
102
- //When at last item, we need to change to 2nd item, so we can scroll right or left
103
- pageIndex = 1 ;
104
- }
105
- else if ( pageIndex == 0 )
122
+ //since we added one item at the beginning of our ItemSource, we need to subtract one
123
+ carouselPosition = pageIndex - 1 ;
124
+
125
+ if ( ItemsView . Position != carouselPosition )
126
+ {
127
+ //If we are updating the ItemsSource, we don't want to scroll the CollectionView
128
+ if ( cv2Controller . IsUpdating ( ) )
106
129
{
107
- //When at first item, need to change to one before last, so we can scroll right or left
108
- pageIndex = maxIndex - 1 ;
130
+ return ;
109
131
}
110
132
111
- //since we added one item at the beginning, we need to subtract one
112
- var realPage = pageIndex - 1 ;
133
+ var goToIndexPath = cv2Controller . GetScrollToIndexPath ( carouselPosition ) ;
113
134
114
- if ( currentPosition != realPage )
115
- {
116
- currentPosition = realPage ;
117
- var pageNumberIndex = NSIndexPath . FromRowSection ( pageIndex , 0 ) ;
118
- Controller . CollectionView . ScrollToItem ( pageNumberIndex , UICollectionViewScrollPosition . Left , false ) ;
135
+ //This will move the carousel to fake the loop
136
+ Controller . CollectionView . ScrollToItem ( NSIndexPath . FromItemSection ( pageIndex , 0 ) , UICollectionViewScrollPosition . Left , false ) ;
119
137
120
- //Update the CarouselView position
121
- ( Controller as CarouselViewController2 ) ? . SetPosition ( realPage ) ;
122
- }
123
- }
124
- else
125
- {
126
- ( Controller as CarouselViewController2 ) ? . SetPosition ( ( int ) page ) ;
127
138
}
128
139
}
129
- } ;
140
+
141
+ //Update the CarouselView position
142
+ cv2Controller ? . SetPosition ( carouselPosition ) ;
130
143
144
+ } ;
131
145
return section ;
132
146
} ) ;
133
147
0 commit comments