@@ -21,10 +21,14 @@ import androidx.compose.ui.Modifier
2121import androidx.compose.ui.draw.drawWithContent
2222import androidx.compose.ui.graphics.Brush
2323import androidx.compose.ui.graphics.Color
24+ import androidx.compose.ui.graphics.graphicsLayer
2425import androidx.compose.ui.unit.Dp
2526import androidx.compose.ui.unit.dp
27+ import androidx.compose.ui.util.lerp
2628import com.google.accompanist.pager.ExperimentalPagerApi
2729import com.google.accompanist.pager.HorizontalPager
30+ import com.google.accompanist.pager.calculateCurrentOffsetForPage
31+ import kotlin.math.absoluteValue
2832
2933@OptIn(ExperimentalPagerApi ::class )
3034@Composable
@@ -35,6 +39,10 @@ fun ExpandableHorizontalPager(
3539 itemSpacing : Dp = 0.dp,
3640 initialHorizontalPadding : Dp = 64.dp,
3741 targetHorizontalPadding : Dp = 0.dp,
42+ outerItemScaleEnabled : Boolean = true,
43+ outerItemScale : Float = 0.85f,
44+ outerItemAlphaEnabled : Boolean = true,
45+ outerItemAlpha : Float = 0.5f,
3846 key : ((page: Int ) -> Any )? = null,
3947 userScrollEnabled : Boolean = true,
4048 initialWidth : Dp = 200.dp,
@@ -146,7 +154,6 @@ fun ExpandableHorizontalPager(
146154 expanded = ! expanded
147155 }
148156
149- var expandedPage by remember { mutableStateOf(0 ) }
150157 HorizontalPager (
151158 count = count,
152159 modifier = modifier,
@@ -163,24 +170,34 @@ fun ExpandableHorizontalPager(
163170 ) {
164171 Card (
165172 modifier = Modifier
166- .width(
167- if (expandedPage == page) {
168- contentWidth
169- } else {
170- initialWidth
171- }
172- )
173- .height(
174- if (expandedPage == page) {
175- boxHeight
176- } else {
177- 0 .dp
178- }
179- )
173+ .width(contentWidth)
174+ .height(if (expanded) boxHeight else 0 .dp)
180175 .offset(
181176 x = 0 .dp,
182177 y = boxOffSetY
183- ),
178+ )
179+ .graphicsLayer {
180+ if (! expanded) {
181+ val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue
182+ if (outerItemScaleEnabled) {
183+ lerp(
184+ start = outerItemScale,
185+ stop = 1f ,
186+ fraction = 1f - pageOffset.coerceIn(0f , 1f )
187+ ).also { scale ->
188+ scaleX = scale
189+ scaleY = scale
190+ }
191+ }
192+ if (outerItemAlphaEnabled) {
193+ alpha = lerp(
194+ start = outerItemAlpha,
195+ stop = 1f ,
196+ fraction = 1f - pageOffset.coerceIn(0f , 1f )
197+ )
198+ }
199+ }
200+ },
184201 shape = RoundedCornerShape (cornerSize),
185202 colors = CardDefaults .cardColors(
186203 containerColor = Color .Black .copy(0.95f ),
@@ -210,35 +227,46 @@ fun ExpandableHorizontalPager(
210227 }
211228 Card (
212229 modifier = Modifier
213- .width(
214- if (expandedPage == page) {
215- contentWidth
216- } else {
217- initialWidth
218- }
219- )
220- .height(
221- if (expandedPage == page) {
222- contentWidth * 1 / aspectRatio
223- } else {
224- initialWidth * 1 / aspectRatio
225- }
226- )
230+ .width(contentWidth)
231+ .height(contentWidth * 1 / aspectRatio)
227232 .aspectRatio(aspectRatio)
228233 .offset(
229- x = if (expandedPage == page) contentOffSetX else 0 .dp ,
230- y = if (expandedPage == page) contentOffSetY else 0 .dp
234+ x = contentOffSetX,
235+ y = contentOffSetY
231236 )
237+ .graphicsLayer {
238+ if (! expanded) {
239+ val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue
240+ if (outerItemScaleEnabled) {
241+ lerp(
242+ start = outerItemScale,
243+ stop = 1f ,
244+ fraction = 1f - pageOffset.coerceIn(0f , 1f )
245+ ).also { scale ->
246+ scaleX = scale
247+ scaleY = scale
248+ }
249+ }
250+ if (outerItemAlphaEnabled) {
251+ alpha = lerp(
252+ start = outerItemAlpha,
253+ stop = 1f ,
254+ fraction = 1f - pageOffset.coerceIn(0f , 1f )
255+ )
256+ }
257+ }
258+ }
232259 .draggable(
260+ enabled = currentPage == page,
233261 orientation = Orientation .Vertical ,
234262 state = rememberDraggableState {},
235263 onDragStarted = {
236- expandedPage = page
237264 expand(maxHeight)
238265 }
239266 )
240- .clickable {
241- expandedPage = page
267+ .clickable(
268+ enabled = currentPage == page,
269+ ) {
242270 expand(maxHeight)
243271 },
244272 shape = RoundedCornerShape (cornerSize)
0 commit comments