Функция rememberDraggableState
Функция rememberDraggableState
возвращает
DraggableState
и держит его в памяти composable.
В лямбду передают обработчик delta:
при каждом шаге жеста приходит смещение по оси.
Полученный state передают в
draggable.
Синтаксис
val dragState = rememberDraggableState { delta ->
offsetX += delta
}
Параметры
| Параметр | Описание |
|---|---|
onDelta |
Лямбда с delta за один шаг перетаскивания. Обычно накапливают смещение в переменной. |
Пример
Давайте сдвинем текст по горизонтали через draggable:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var offsetX by remember { mutableFloatStateOf(0f) }
val dragState = rememberDraggableState { delta ->
offsetX += delta
}
Text(
text = "abcde",
modifier = Modifier
.offset { IntOffset(offsetX.roundToInt(), 0) }
.draggable(
orientation = Orientation.Horizontal,
state = dragState,
)
.background(Color.LightGray)
.padding(16.dp),
)
}
}
}
На экране плашку можно тянуть влево и вправо, offset растёт из колбэка rememberDraggableState.
Пример
Давайте тянуть плашку по вертикали:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.offset
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import kotlin.math.roundToInt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var offsetY by remember { mutableFloatStateOf(0f) }
val dragState = rememberDraggableState { delta ->
offsetY += delta
}
Text(
text = "12345",
modifier = Modifier
.offset { IntOffset(0, offsetY.roundToInt()) }
.draggable(
orientation = Orientation.Vertical,
state = dragState,
),
)
}
}
}
На экране текст "12345" будет двигаться
вверх и вниз за пальцем.
Смотрите также
-
класс
DraggableState,
который возвращает rememberDraggableState -
метод
draggable,
который использует этот state -
метод
dispatchRawDelta,
куда draggable передаёт delta -
метод
anchoredDraggable,
который тянет элемент между якорями