Skip to content

Commit a2d3a1c

Browse files
committed
horizontal padding added.
1 parent 9f82d0c commit a2d3a1c

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

expandable-horizontal-pager/src/main/java/com/commandiron/expandable_horizontal_pager/ExpandableHorizontalPager.kt

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ fun ExpandableHorizontalPager(
3333
modifier: Modifier = Modifier,
3434
reverseLayout: Boolean = false,
3535
itemSpacing: Dp = 0.dp,
36-
contentPadding: PaddingValues = PaddingValues(0.dp),
37-
verticalAlignment: Alignment.Vertical = Alignment.CenterVertically,
36+
initialHorizontalPadding: Dp = 64.dp,
37+
targetHorizontalPadding: Dp = 0.dp,
3838
key: ((page: Int) -> Any)? = null,
3939
userScrollEnabled: Boolean = true,
4040
initialWidth: Dp = 200.dp,
@@ -103,8 +103,18 @@ fun ExpandableHorizontalPager(
103103
)
104104
)
105105

106+
var horizontalPaddingState by remember { mutableStateOf(initialHorizontalPadding) }
107+
val horizontalPadding by animateDpAsState(
108+
targetValue = horizontalPaddingState,
109+
animationSpec = tween(
110+
durationMillis = durationMillis
111+
)
112+
)
113+
106114
fun expand(maxHeight: Dp) {
107115
if (!expanded) {
116+
horizontalPaddingState = targetHorizontalPadding
117+
108118
contentWidthState = targetWidth
109119
contentOffSetYState =
110120
-((maxHeight - (contentWidthState * 1 / aspectRatio)) / 2)
@@ -123,6 +133,8 @@ fun ExpandableHorizontalPager(
123133

124134
cornerSizeState = 0.dp
125135
} else {
136+
horizontalPaddingState = initialHorizontalPadding
137+
126138
contentWidthState = initialWidth
127139
contentOffSetYState = 0.dp
128140

@@ -134,13 +146,13 @@ fun ExpandableHorizontalPager(
134146
expanded = !expanded
135147
}
136148

149+
var expandedPage by remember { mutableStateOf(0) }
137150
HorizontalPager(
138151
count = count,
139152
modifier = modifier,
140153
reverseLayout = reverseLayout,
141154
itemSpacing = itemSpacing,
142-
contentPadding = contentPadding,
143-
verticalAlignment = verticalAlignment,
155+
contentPadding = PaddingValues(horizontal = horizontalPadding),
144156
key = key,
145157
userScrollEnabled = userScrollEnabled
146158
) { page ->
@@ -151,8 +163,20 @@ fun ExpandableHorizontalPager(
151163
) {
152164
Card(
153165
modifier = Modifier
154-
.width(contentWidth)
155-
.height(boxHeight)
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+
)
156180
.offset(
157181
x = 0.dp,
158182
y = boxOffSetY
@@ -186,16 +210,37 @@ fun ExpandableHorizontalPager(
186210
}
187211
Card(
188212
modifier = Modifier
189-
.width(contentWidth)
190-
.height(contentWidth * 1 / aspectRatio)
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+
)
191227
.aspectRatio(aspectRatio)
192-
.offset(x = contentOffSetX, y = contentOffSetY)
228+
.offset(
229+
x = if(expandedPage == page) contentOffSetX else 0.dp,
230+
y = if(expandedPage == page) contentOffSetY else 0.dp
231+
)
193232
.draggable(
194233
orientation = Orientation.Vertical,
195234
state = rememberDraggableState {},
196-
onDragStarted = { expand(maxHeight) }
235+
onDragStarted = {
236+
expandedPage = page
237+
expand(maxHeight)
238+
}
197239
)
198-
.clickable { expand(maxHeight) },
240+
.clickable {
241+
expandedPage = page
242+
expand(maxHeight)
243+
},
199244
shape = RoundedCornerShape(cornerSize)
200245
) {
201246
Box() {

0 commit comments

Comments
 (0)