Foe*_*nix 1 android android-jetpack-compose
我调查了 boguszpawlowski Compose Calendar,但找不到跟踪组件状态更改的方法,因为它没有回调。例如,我只需要获取当月的事件(发送请求),之后我应该在日历中显示它们。当用户切换月份时,我应该再次发送请求。描述中的作者建议使用状态提升。
如果您需要对状态更改做出反应,或者从可组合项外部更改状态,则需要将状态从 Calendar 可组合项中提升出来:
@Composable
fun MainScreen() {
val calendarState = rememberCalendarState()
StaticCalendar(calendarState = calendarState)
// now you can manipulate the state from scope of this composable
calendarState.monthState.currentMonth = MonthYear.of(2020, 5)
}
Run Code Online (Sandbox Code Playgroud)
我了解如何更改当前的初始月份,但我不知道当用户滑动日历或单击按钮更改月份时如何监听事件。我该怎么做?
你可以使用LaunchedEffect监听状态变化 我没有使用你的库,但你可以将它传递给 LaunchedEffect
LaunchedEffect(calendarState){
// your conditions
// do your api call here
}
Run Code Online (Sandbox Code Playgroud)
或者
LaunchedEffect(calendarState.monthState.currentMonth){
// do your api call here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1316 次 |
| 最近记录: |