@@ -5,6 +5,7 @@ import androidx.compose.foundation.gestures.BringIntoViewSpec
5
5
import androidx.compose.foundation.gestures.LocalBringIntoViewSpec
6
6
import androidx.compose.runtime.Composable
7
7
import androidx.compose.runtime.CompositionLocalProvider
8
+ import androidx.compose.runtime.remember
8
9
9
10
@OptIn(ExperimentalFoundationApi ::class )
10
11
@Composable
@@ -14,32 +15,34 @@ fun PositionFocusedItemInLazyLayout(
14
15
content : @Composable () -> Unit ,
15
16
) {
16
17
// This bring-into-view spec pivots around the center of the scrollable container
17
- val bringIntoViewSpec = object : BringIntoViewSpec {
18
- override fun calculateScrollDistance (
19
- // Initial position of item requesting focus
20
- offset : Float ,
21
- // Size of item requesting focus
22
- size : Float ,
23
- // Size of the lazy container
24
- containerSize : Float
25
- ): Float {
26
- val childSmallerThanParent = size <= containerSize
27
- val initialTargetForLeadingEdge =
28
- parentFraction * containerSize - (childFraction * size)
29
- val spaceAvailableToShowItem = containerSize - initialTargetForLeadingEdge
18
+ val bringIntoViewSpec = remember(parentFraction, childFraction) {
19
+ object : BringIntoViewSpec {
20
+ override fun calculateScrollDistance (
21
+ // initial position of item requesting focus
22
+ offset : Float ,
23
+ // size of item requesting focus
24
+ size : Float ,
25
+ // size of the lazy container
26
+ containerSize : Float
27
+ ): Float {
28
+ val childSmallerThanParent = size <= containerSize
29
+ val initialTargetForLeadingEdge =
30
+ parentFraction * containerSize - (childFraction * size)
31
+ val spaceAvailableToShowItem = containerSize - initialTargetForLeadingEdge
30
32
31
- val targetForLeadingEdge =
32
- if (childSmallerThanParent && spaceAvailableToShowItem < size) {
33
- containerSize - size
34
- } else {
35
- initialTargetForLeadingEdge
36
- }
33
+ val targetForLeadingEdge =
34
+ if (childSmallerThanParent && spaceAvailableToShowItem < size) {
35
+ containerSize - size
36
+ } else {
37
+ initialTargetForLeadingEdge
38
+ }
37
39
38
- return offset - targetForLeadingEdge
40
+ return offset - targetForLeadingEdge
41
+ }
39
42
}
40
43
}
41
44
42
- // LocalBringIntoViewSpec will apply to all scrollables in the hierarchy
45
+ // LocalBringIntoViewSpec will apply to all scrollables in the hierarchy.
43
46
CompositionLocalProvider (
44
47
LocalBringIntoViewSpec provides bringIntoViewSpec,
45
48
content = content,
0 commit comments