为什么我使用纯组件但仍然需要片段依赖?

huangapple go评论73阅读模式
英文:

Why do I use pure components but still need fragment dependencies?

问题

我在使用Compose时遇到了问题,然后我找到了答案

如果您在使用Compose与Fragments时,可能没有在定义viewModels()的地方添加Fragments依赖。

添加:

implementation "androidx.fragment:fragment-ktx:1.5.2"

我正在使用Compose与Fragments,但我使用纯Compose也遇到了这个问题。我错过了什么吗?或者Fragment和Compose之间有什么联系吗?

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    private val userViewModel: UserViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Content(userViewModel)
        }
    }
}

@Composable
fun Content(userViewModel: UserViewModel) {

    val lazyArticleItem = userViewModel.list().collectAsLazyPagingItems()

    thread {
        repeat(200) {
            userViewModel.insert(User())
        }
    }

    LazyColumn(verticalArrangement = Arrangement.spacedBy(16.dp)) {
        items(lazyArticleItem) { user ->
            Text("user ${user?.id}")
        }
    }

}

以上是我的UI界面代码,基于这个,我认为我没有使用Fragment。

我想声明我的逻辑。我使用的是纯Compose而不是Fragment,但实际上要运行这段代码必须依赖于androidx.fragment:fragment-ktx:1.5.2

英文:

I have a problem when using compose, then i found the answer
> If you use Compose with Fragments, then you may not have the Fragments dependency where viewModels() is defined.
>
> Adding:
>
> implementation "androidx.fragment:fragment-ktx:1.5.2"

use Compose with Fragments, but I use Pure Compose, Also had this problem.
What am I missing? Or is there some connection between fragment and compose?


@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    private val userViewModel: UserViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Content(userViewModel)
        }
    }
}

@Composable
fun Content(userViewModel: UserViewModel) {

    val lazyArticleItem = userViewModel.list().collectAsLazyPagingItems()

    thread {
        repeat(200) {
            userViewModel.insert(User())
        }
    }

    LazyColumn(verticalArrangement = Arrangement.spacedBy(16.dp)) {
        items(lazyArticleItem) { user ->
            Text("user ${user?.id}")
        }
    }

}

The above is my ui interface code, based on this, I don't think I'm using fragment.

I want to declare my logic. I use Pure Compose instead of Fragment, but actually want to run the code must depend on androidx.fragment:fragment-ktx:1.5.2

答案1

得分: 2

发生这种情况是因为您正在使用

val userViewModel: UserViewModel by viewModels()

您可以通过调用viewModel()函数从任何可组合项访问ViewModel。
使用:

val userViewModel: UserViewModel = viewModel()

要使用viewModel()函数,请添加androidx.lifecycle:lifecycle-viewmodel-compose:x.x.x

英文:

It happens because you are using

val userViewModel: UserViewModel by viewModels()

You can access a ViewModel from any composable by calling the viewModel() function.
Use:

val userViewMode : UserViewModel = viewModel()

To use the viewModel() functions, add the androidx.lifecycle:lifecycle-viewmodel-compose:x.x.x

答案2

得分: -1

在编程中,"fragment""compose" 可以指代两个相关的概念:

Fragment:在UI设计中,一个片段是一个活动的UI的一部分,可以在多个活动中重复使用或组合在一起形成一个单一的活动。它提供了一种模块化UI并使其更易管理的方式。

Compose:Compose是由Google推出的用于Android应用开发的现代UI工具包,允许开发人员使用可组合的函数构建和样式化UI元素。它提供了一种创建和重用可以组合成完整应用程序UI的UI组件的方式。

这两个概念都旨在使UI设计和开发更加模块化、可重用和可维护。

希望这有所帮助!

英文:

In programming, "fragment" and "compose" can refer to two related concepts:

Fragment: In UI design, a fragment is a portion of an activity's UI, which can be reused in multiple activities or combined to form a single activity. It provides a way to modularize the UI and make it more manageable.

Compose: Compose is a modern UI toolkit for Android app development introduced by Google, which allows developers to build and style UI elements using composable functions. It provides a way to create and reuse UI components that can be combined to form a complete app UI.

Both concepts are aimed at making UI design and development more modular, reusable and maintainable.

Hope this helps!

huangapple
  • 本文由 发表于 2023年2月8日 13:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定