gpt4 book ai didi

android - 未调用 LiveData 观察者

转载 作者:IT老高 更新时间:2023-10-28 13:44:14 24 4
gpt4 key购买 nike

我有一个 Activity TabBarActivity 托管一个 fragment EquipmentRecyclerViewFragment。 fragment 接收 LiveData 回调,但 Activity 没有(如 Debug模式下的断点所证明的那样)。奇怪的是,如果我调用 ViewModel 的 initData 方法,Activity 回调确实会触发。以下是上述组件的相关部分:

TabBarActivity

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initVM()
setContentView(R.layout.activity_nav)
val equipmentRecyclerViewFragment = EquipmentRecyclerViewFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.frameLayout, equipmentRecyclerViewFragment, equipmentRecyclerViewFragment.TAG)
.commit()
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

}

var eVM : EquipmentViewModel? = null
private fun initVM() {
eVM = ViewModelProviders.of(this).get(EquipmentViewModel::class.java)
eVM?.let { lifecycle.addObserver(it) } //Add ViewModel as an observer of this fragment's lifecycle
eVM?.equipment?.observe(this, loadingObserver)// eVM?.initData() //TODO: Not calling this causes Activity to never receive the observed ∆
}
val loadingObserver = Observer<List<Gun>> { equipment ->
...}

EquipmentRecyclerViewFragment

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
columnCount = 2
initVM()
}

//MARK: ViewModel Methods
var eVM : EquipmentViewModel? = null
private fun initVM() {
eVM = ViewModelProviders.of(this).get(EquipmentViewModel::class.java)
eVM?.let { lifecycle.addObserver(it) } //Add ViewModel as an observer of this fragment's lifecycle
eVM?.equipment?.observe(this, equipmentObserver)
eVM?.initData()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_equipment_list, container, false)
if (view is RecyclerView) { // Set the adapter
val context = view.getContext()
view.layoutManager = GridLayoutManager(context, columnCount)
view.adapter = adapter
}
return view
}

设备 View 模型

class EquipmentViewModel(application: Application) : AndroidViewModel(application), LifecycleObserver {
var equipment = MutableLiveData<List<Gun>>()
var isLoading = MutableLiveData<Boolean>()

fun initData() {
isLoading.setValue(true)
thread { Thread.sleep(5000) //Simulates async network call
var gunList = ArrayList<Gun>()
for (i in 0..100){
gunList.add(Gun("Gun "+i.toString()))
}
equipment.postValue(gunList)
isLoading.postValue(false)
}
}

最终目的是让 Activity 只观察 isLoading MutableLiveData bool 值,但由于这不起作用,我将 Activity 更改为只观察设备 LiveData 以尽量减少变量的数量.

最佳答案

要获得与 Activity 相同的 ViewModel 引用,您需要传递相同的 Activity 实例,您应该使用 ViewModelProviders。的(getActivity)。当您将 this 作为参数传递时,您会收到与您的 Fragment 关联的 ViewModel 实例。

有两种重载方法:

ViewModelProvider.of(Fragment fragment)

ViewModelProvider.of(FragmentActivity activity)

更多信息Share data between fragments

关于android - 未调用 LiveData 观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48748301/

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