hostAction / combinedAction · combinedAction
Change local state and notify the host in one tap.
Открыть живое интерактивное демо →
Меняйте параметры, смотрите удалённый документ, который получается, и разницу после каждого изменения.
Что это
Нажатие, которое сообщает приложению вокруг экрана, что нужно сделать: открыть страницу или сохранить закладку, — заодно обновляя экран.
Подходит для
- Переходы на экраны приложения
- Кнопки «нравится», «в закладки», «поделиться»
Код
Исходник примера с импортами androidx.compose.remote. CI компилирует каждый фрагмент кода на сайте против AndroidX.
import androidx.compose.remote.creation.compose.action.combinedAction
import androidx.compose.remote.creation.compose.action.hostAction
import androidx.compose.remote.creation.compose.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clickable
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.padding
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.state.MutableRemoteBoolean
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun CombinedActions(actionName: String) {
val isBookmarked = remember { MutableRemoteBoolean(false) }
val next = !isBookmarked
RemoteBox(
RemoteModifier.clip(RemoteRoundedCornerShape(12.rdp))
.background(isBookmarked.select(Color(0xFF2E7D32).rc, Color(0xFF616161).rc))
// Update local state and tell the host, in one tap.
.clickable(
combinedAction(
valueChangeAction(isBookmarked, next),
hostAction(actionName.rs, next),
),
)
.padding(16.rdp),
) {
RemoteText(
isBookmarked.select("Bookmarked".rs, "Tap to bookmark".rs),
color = Color.White.rc,
)
}
}
API
creation.compose.action.hostAction
RemoteAction hostAction(RemoteString name)
RemoteAction hostAction(RemoteString name, RemoteBoolean value)
RemoteAction hostAction(RemoteString name, RemoteFloat value)
RemoteAction hostAction(RemoteString name, RemoteInt value)
RemoteAction hostAction(RemoteString name, RemoteString value)
RemoteAction hostAction(int id, optional RemoteString? metadata)creation.compose.action.combinedAction
RemoteAction combinedAction(RemoteAction... actions)creation.compose.action.RemoteAction
interface RemoteAction
RemoteAction.Companion Companion
RemoteAction None