如何消除将视图模型传递给此 Jetpack Compose Kotlin 主屏幕?
How to eliminate passing View Model to this Jetpack Compose Kotlin Home Screen?
我正在尝试清理我的代码并删除不必要的视图模型。
我希望能够在我的 HomeScreen
函数中访问 itemList
、filtering
和 itemListFiltered
,而无需显式传递 MainViewModel
作为参数,但我不知道该怎么做。
我尝试使用 itemList = model::itemList
但 Android Studio 给出错误:Type mismatch. Required: List<Int> Found: KProperty0<SnapshotStateList<Int>>
查看模型
class MainViewModel : ViewModel() {
val itemList = mutableStateListOf<Int>()
val filtering = mutableStateOf<Boolean>(false)
val itemListFiltered = mutableStateListOf<Int>()
fun addItem(item: Int) {
itemList.add(item)
}
fun clearItemList() {
itemList.clear()
}
fun filterListGreaterThan(greaterThan: Int) {
itemListFiltered.clear()
itemList.forEach { item ->
if (item > greaterThan) itemListFiltered.add(item)
}
}
init {
clearItemList()
} // End Initializer
}
主屏幕脚手架
@Composable
fun HomeScreen(
model: MainViewModel // <-- How do I eliminate this
) {
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Closed))
Scaffold(
scaffoldState = scaffoldState,
topBar = { TopBar(scope, scaffoldState) },
floatingActionButtonPosition = FabPosition.End,
floatingActionButton = {
FloatingActionButton(
onAddItem = model::addItem
) },
drawerContent = {
DrawerContent(
onAddItem = model::addItem,
onResetList = model::clearItemList
) },
drawerGesturesEnabled = true,
content = {
Content(
itemList = model.itemList, // <-- How do I pass this itemList without model?
onAddItem = model::addItem
) },
bottomBar = { BottomBar() }
)
}
你能帮我看看怎么做吗?
感谢您的帮助!
就这样传下去...
model.itemsList
::
符号并不像您认为的那样起作用。考虑将其用于静态 non-changing variables/methods。就像您可以使用 model::addItem
或 model::clearItemList
一样,因为它们只是 public 方法并且不包含值。
我正在尝试清理我的代码并删除不必要的视图模型。
我希望能够在我的 HomeScreen
函数中访问 itemList
、filtering
和 itemListFiltered
,而无需显式传递 MainViewModel
作为参数,但我不知道该怎么做。
我尝试使用 itemList = model::itemList
但 Android Studio 给出错误:Type mismatch. Required: List<Int> Found: KProperty0<SnapshotStateList<Int>>
查看模型
class MainViewModel : ViewModel() {
val itemList = mutableStateListOf<Int>()
val filtering = mutableStateOf<Boolean>(false)
val itemListFiltered = mutableStateListOf<Int>()
fun addItem(item: Int) {
itemList.add(item)
}
fun clearItemList() {
itemList.clear()
}
fun filterListGreaterThan(greaterThan: Int) {
itemListFiltered.clear()
itemList.forEach { item ->
if (item > greaterThan) itemListFiltered.add(item)
}
}
init {
clearItemList()
} // End Initializer
}
主屏幕脚手架
@Composable
fun HomeScreen(
model: MainViewModel // <-- How do I eliminate this
) {
val scope = rememberCoroutineScope()
val scaffoldState = rememberScaffoldState(rememberDrawerState(DrawerValue.Closed))
Scaffold(
scaffoldState = scaffoldState,
topBar = { TopBar(scope, scaffoldState) },
floatingActionButtonPosition = FabPosition.End,
floatingActionButton = {
FloatingActionButton(
onAddItem = model::addItem
) },
drawerContent = {
DrawerContent(
onAddItem = model::addItem,
onResetList = model::clearItemList
) },
drawerGesturesEnabled = true,
content = {
Content(
itemList = model.itemList, // <-- How do I pass this itemList without model?
onAddItem = model::addItem
) },
bottomBar = { BottomBar() }
)
}
你能帮我看看怎么做吗?
感谢您的帮助!
就这样传下去...
model.itemsList
::
符号并不像您认为的那样起作用。考虑将其用于静态 non-changing variables/methods。就像您可以使用 model::addItem
或 model::clearItemList
一样,因为它们只是 public 方法并且不包含值。