gpt4 book ai didi

android - 使用 Factory 创建共享 ViewModel

转载 作者:行者123 更新时间:2023-12-03 08:10:54 29 4
gpt4 key购买 nike

我有一个带有 3 个选项卡的 TabLayout 的 DocumentsFragment:

TabRulesFragment,TabProceduresFragment,TabGuidanceFragment

在 DocumentsFragment 中,我使用工厂初始化共享 viewModel、DocumentsSharedViewModel:

class DocumentsFragment : Fragment() {

private lateinit var sharedViewModel: DocumentsSharedViewModel
private lateinit var viewPager2: ViewPager2
private lateinit var documentsCollectionAdapter: DocumentsCollectionAdapter

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val program = DocumentsFragmentArgs.fromBundle(requireArguments()).program
val name = DocumentsFragmentArgs.fromBundle(requireArguments()).name

val viewModelFactory = DocumentsSharedViewModelFactory(program, name)
sharedViewModel = ViewModelProvider(this, viewModelFactory)[DocumentsSharedViewModel::class.java]

在文档 fragment 和 3 个选项卡 fragment 之间共享数据。当我尝试连接到选项卡 fragment 之一(例如 TabRulesFragment)中的共享 viewModel 时:

class TabRulesFragment : Fragment() {

private lateinit var tabRulesRecyclerView: RecyclerView

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {

val sharedViewModel : DocumentsSharedViewModel by viewModels()
val binding = TabRulesFragmentBinding.inflate(layoutInflater)
binding.viewModel = sharedViewModel

我收到错误消息,无法创建 DocumentsSharedViewModel 的实例:

java.lang.RuntimeException: Cannot create an instance of class com.smellydogcoding.westvirginiaelectronicfieldguide.ui.documents.DocumentsSharedViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:188)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:238)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:169)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:44)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:31)
at com.smellydogcoding.westvirginiaelectronicfieldguide.ui.documents.rulesTab.TabRulesFragment.onCreateView$lambda-0(TabRulesFragment.kt:30)
at com.smellydogcoding.westvirginiaelectronicfieldguide.ui.documents.rulesTab.TabRulesFragment.onCreateView(TabRulesFragment.kt:32)

我假设 viewModelProvider 正在寻找工厂(它在 TabRulesFragment 中不存在,因为它在 DocumentsFragment 中),并在找不到它时抛出错误。有没有办法使用共享数据模型中的数据而不创建它的另一个实例?

最佳答案

如果您希望将 ViewModel 范围限定为可以在 fragment 之间共享的所属 Activity,则可以使用以下命令在两个 fragment 中获取它。

val sharedModel: DocumentsSharedViewModel by activityViewModels()

根据docs ,其中有一个简单的示例,其中两个 fragment 都可以访问相同的 ViewModel

class ListFragment : Fragment() {

// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: SharedViewModel by activityViewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//...
}
}

class DetailFragment : Fragment() {

// Use the 'by activityViewModels()' Kotlin property delegate
// from the fragment-ktx artifact
private val model: SharedViewModel by activityViewModels()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//...
}
}

正如评论中所指出的,如果您希望 ViewModel 的范围保持在 parent fragment 范围内,您可以使用它来在子 fragment 中访问它,而不是使用 Activity 范围

val sharedModel: DocumentsSharedViewModel by viewModels({ requireParentFragment() })

关于android - 使用 Factory 创建共享 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70855873/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com