-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add comment refactor longPressDraggableHandle minor fixes
- Loading branch information
Showing
6 changed files
with
330 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
demoApp/src/main/java/sh/calvin/reorderable/LongPressHandleReorderableColumnScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package sh.calvin.reorderable | ||
|
||
import android.os.Build | ||
import android.view.HapticFeedbackConstants | ||
import androidx.compose.animation.core.animateDpAsState | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.rounded.DragHandle | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.key | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun LongPressHandleReorderableColumnScreen() { | ||
val view = LocalView.current | ||
|
||
var list by remember { mutableStateOf(items.take(5)) } | ||
|
||
ReorderableColumn( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(8.dp), | ||
list = list, | ||
onSettle = { fromIndex, toIndex -> | ||
list = list.toMutableList().apply { | ||
add(toIndex, removeAt(fromIndex)) | ||
} | ||
}, | ||
onMove = { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK) | ||
} | ||
}, | ||
verticalArrangement = Arrangement.spacedBy(8.dp), | ||
) { _, item, isDragging -> | ||
key(item.id) { | ||
val elevation by animateDpAsState(if (isDragging) 4.dp else 0.dp) | ||
|
||
Card( | ||
modifier = Modifier.height(item.size.dp), | ||
shadowElevation = elevation, | ||
) { | ||
Text(item.text, Modifier.padding(horizontal = 8.dp)) | ||
IconButton( | ||
modifier = Modifier.longPressDraggableHandle( | ||
onDragStarted = { | ||
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) | ||
}, | ||
onDragStopped = { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END) | ||
} | ||
}, | ||
), | ||
onClick = {}, | ||
) { | ||
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder") | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...p/src/main/java/sh/calvin/reorderable/SimpleLongPressHandleReorderableLazyColumnScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package sh.calvin.reorderable | ||
|
||
import android.os.Build | ||
import android.view.HapticFeedbackConstants | ||
import androidx.compose.animation.core.animateDpAsState | ||
import androidx.compose.foundation.ExperimentalFoundationApi | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.foundation.lazy.rememberLazyListState | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.rounded.DragHandle | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalView | ||
import androidx.compose.ui.unit.dp | ||
|
||
@OptIn(ExperimentalFoundationApi::class) | ||
@Composable | ||
fun SimpleLongPressHandleReorderableLazyColumnScreen() { | ||
val view = LocalView.current | ||
|
||
var list by remember { mutableStateOf(items) } | ||
val lazyListState = rememberLazyListState() | ||
val reorderableLazyColumnState = rememberReorderableLazyColumnState(lazyListState) { from, to -> | ||
list = list.toMutableList().apply { | ||
add(to.index, removeAt(from.index)) | ||
} | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
view.performHapticFeedback(HapticFeedbackConstants.SEGMENT_FREQUENT_TICK) | ||
} | ||
} | ||
|
||
LazyColumn( | ||
modifier = Modifier.fillMaxSize(), | ||
state = lazyListState, | ||
contentPadding = PaddingValues(8.dp), | ||
verticalArrangement = Arrangement.spacedBy(8.dp) | ||
) { | ||
items(list, key = { it.id }) { | ||
ReorderableItem(reorderableLazyColumnState, it.id) { isDragging -> | ||
val elevation by animateDpAsState(if (isDragging) 4.dp else 0.dp) | ||
|
||
Card( | ||
modifier = Modifier.height(it.size.dp), | ||
shadowElevation = elevation, | ||
) { | ||
Text(it.text, Modifier.padding(horizontal = 8.dp)) | ||
IconButton( | ||
modifier = Modifier.longPressDraggableHandle( | ||
onDragStarted = { | ||
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) | ||
}, | ||
onDragStopped = { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
view.performHapticFeedback(HapticFeedbackConstants.GESTURE_END) | ||
} | ||
}, | ||
), | ||
onClick = {}, | ||
) { | ||
Icon(Icons.Rounded.DragHandle, contentDescription = "Reorder") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.