@@ -58,12 +58,24 @@ export const StickyHeaders = <TItem,>({
58
58
data,
59
59
extraData,
60
60
} : StickyHeaderProps < TItem > ) => {
61
- const [ stickyIndices , setStickyIndices ] = useState < {
61
+ const [ stickyHeaderState , setStickyHeaderState ] = useState < {
62
62
currentStickyIndex : number ;
63
63
nextStickyIndex : number ;
64
- } > ( { currentStickyIndex : - 1 , nextStickyIndex : - 1 } ) ;
65
-
66
- const { currentStickyIndex, nextStickyIndex } = stickyIndices ;
64
+ currentStickyHeight : number ;
65
+ nextStickyY : number ;
66
+ } > ( {
67
+ currentStickyIndex : - 1 ,
68
+ nextStickyIndex : - 1 ,
69
+ currentStickyHeight : 0 ,
70
+ nextStickyY : 0 ,
71
+ } ) ;
72
+
73
+ const {
74
+ currentStickyIndex,
75
+ nextStickyIndex,
76
+ nextStickyY,
77
+ currentStickyHeight,
78
+ } = stickyHeaderState ;
67
79
68
80
// Memoize sorted indices based on their Y positions
69
81
const sortedIndices = useMemo ( ( ) => {
@@ -79,12 +91,12 @@ export const StickyHeaders = <TItem,>({
79
91
if ( legthInvalid ) {
80
92
return ;
81
93
}
82
- const adjustedValue = recyclerViewManager . getLastScrollOffset ( ) ;
94
+ const adjustedScrollOffset = recyclerViewManager . getLastScrollOffset ( ) ;
83
95
84
96
// Binary search for current sticky index
85
97
const currentIndexInArray = findCurrentStickyIndex (
86
98
sortedIndices ,
87
- adjustedValue ,
99
+ adjustedScrollOffset ,
88
100
( index ) => recyclerViewManager . getLayout ( index ) . y
89
101
) ;
90
102
@@ -95,13 +107,23 @@ export const StickyHeaders = <TItem,>({
95
107
newNextStickyIndex = - 1 ;
96
108
}
97
109
110
+ const newNextStickyY =
111
+ ( recyclerViewManager . tryGetLayout ( newNextStickyIndex ) ?. y ?? 0 ) +
112
+ recyclerViewManager . firstItemOffset ;
113
+ const newCurrentStickyHeight =
114
+ recyclerViewManager . tryGetLayout ( newStickyIndex ) ?. height ?? 0 ;
115
+
98
116
if (
99
117
newStickyIndex !== currentStickyIndex ||
100
- newNextStickyIndex !== nextStickyIndex
118
+ newNextStickyIndex !== nextStickyIndex ||
119
+ newNextStickyY !== nextStickyY ||
120
+ newCurrentStickyHeight !== currentStickyHeight
101
121
) {
102
- setStickyIndices ( {
122
+ setStickyHeaderState ( {
103
123
currentStickyIndex : newStickyIndex ,
104
124
nextStickyIndex : newNextStickyIndex ,
125
+ nextStickyY : newNextStickyY ,
126
+ currentStickyHeight : newCurrentStickyHeight ,
105
127
} ) ;
106
128
}
107
129
} , [
@@ -110,6 +132,8 @@ export const StickyHeaders = <TItem,>({
110
132
recyclerViewManager ,
111
133
sortedIndices ,
112
134
legthInvalid ,
135
+ nextStickyY ,
136
+ currentStickyHeight ,
113
137
] ) ;
114
138
115
139
useEffect ( ( ) => {
@@ -139,20 +163,20 @@ export const StickyHeaders = <TItem,>({
139
163
} ) ;
140
164
}
141
165
142
- const currentLayout = recyclerViewManager . getLayout ( currentStickyIndex ) ;
143
- const nextLayout = recyclerViewManager . getLayout ( nextStickyIndex ) ;
144
-
145
- const pushStartsAt = nextLayout . y - currentLayout . height ;
166
+ const pushStartsAt = nextStickyY - currentStickyHeight ;
146
167
147
168
return scrollY . interpolate ( {
148
- inputRange : [
149
- pushStartsAt + recyclerViewManager . firstItemOffset ,
150
- nextLayout . y + recyclerViewManager . firstItemOffset ,
151
- ] ,
152
- outputRange : [ 0 , - currentLayout . height ] ,
169
+ inputRange : [ pushStartsAt , nextStickyY ] ,
170
+ outputRange : [ 0 , - currentStickyHeight ] ,
153
171
extrapolate : "clamp" ,
154
172
} ) ;
155
- } , [ currentStickyIndex , nextStickyIndex , recyclerViewManager , scrollY ] ) ;
173
+ } , [
174
+ currentStickyHeight ,
175
+ currentStickyIndex ,
176
+ nextStickyIndex ,
177
+ nextStickyY ,
178
+ scrollY ,
179
+ ] ) ;
156
180
157
181
// Memoize header content
158
182
const headerContent = useMemo ( ( ) => {
0 commit comments