如何通过viewModels获取viewModel?(片段-ktx) [英] how to get viewModel by viewModels? (fragment-ktx)

查看:43
本文介绍了如何通过viewModels获取viewModel?(片段-ktx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Activity 及其所有片段使用 Single viewModel.

所以如果必须在所有片段的onActivityCreated中编写此设置代码,则初始化viewmodel

 override fun onActivityCreated(savedInstanceState: Bundle?) {super.onActivityCreated(savedInstanceState)viewModel = ViewModelProviders.of(activity!!).get(NoteViewModel::class.java)}

我正在仔细阅读 Android KTX 扩展页面:(参考此处)

我发现我可以像这样初始化视图模型:

//获取对该 Fragment 范围内的 ViewModel 的引用val viewModel by viewModels()//获取对其活动范围内的 ViewModel 的引用val viewModel by activityViewModels()

所以我在我的 gradle(app) 中添加了以下依赖项:

//ktx android实现 'androidx.core:core-ktx:1.0.2'实现 'androidx.fragment:fragment-ktx:1.0.0'实现androidx.lifecycle:lifecycle-extensions:2.0.0"

但是当我尝试在我的应用程序中使用 viewModels/activityViewModels 时,找不到它们的引用.

我想通过一些基本示例获得有关如何使用这些扩展程序的帮助,我尝试搜索示例并没有找到任何示例.

解决方案

我们终于有了稳定版.

在转到 implementation 'androidx.fragment:fragment-ktx:1.1.0' 后,我遇到了另一个问题.

编译器错误:

<块引用>

无法将使用 JVM 目标 1.8 构建的字节码内联到使用 JVM 目标 1.6 构建

build.gradle(模块:app)

compileOptions {源兼容性 = 1.8目标兼容性 = 1.8}科特林选项{jvmTarget = "1.8"}

参考

应用上述所有内容后,问题已解决.

谢谢大家.

I am working with Single viewModel for the Activity and all of it's fragment.

So to initialise viewmodel if have to write this setup code in onActivityCreated of all the fragment's

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProviders.of(activity!!).get(NoteViewModel::class.java)
    }

I was going thorough the Android KTX extension page:(refer here)

and i found i can initialise view model like this:

    // Get a reference to the ViewModel scoped to this Fragment
    val viewModel by viewModels<MyViewModel>()

    // Get a reference to the ViewModel scoped to its Activity
    val viewModel by activityViewModels<MyViewModel>()

So I added below dependency's to my gradle(app):

    //ktx android
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.fragment:fragment-ktx:1.0.0'
    implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"

But when i try to use viewModels/activityViewModels in my application their reference in not found.

I want help in how to use these extension's with some basic example i tried searching for examples haven't found any.

解决方案

Atlast we we have got stable version.

After moving to implementation 'androidx.fragment:fragment-ktx:1.1.0' i faced another issue.

Compiler Error:

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6

build.gradle (Module:app)

compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }

reference

After applying all the above the issue's is resolved.

Thanks everyone.

这篇关于如何通过viewModels获取viewModel?(片段-ktx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆