我如何实现一种方法来获取和 return LifeCycle
How do I implement a method to get and return the LifeCycle
我对请求 LifeCycle 的 FragmentStateAdapter
有疑问,请帮助我如何在下面的代码行中设置和传递 LifeCycle。
viewPager2.adapter = myViewPagerAdapter(supportFragmentManager, getCurrentLifeCycle())
这里是myViewPagerAdapter
方法
private fun myViewPagerAdapter(fm: FragmentManager, lifeCycle: Lifecycle): RecyclerView.Adapter<*> {
//val items = items // avoids resolving the ViewModel multiple times
return object : FragmentStateAdapter(fm, lifeCycle) {
override fun getItemCount(): Int {
// Show fragments.size total pages.
return fragments.size
}
private val fragments = arrayOf(
MyPostsFragment(),
RecentPostsFragment()
)
override fun createFragment(position: Int): Fragment {
return fragments[position]
}
}
}
经过一番挖掘,我明白了,LifeCycle 是 @NonNull
public FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
@NonNull Lifecycle lifecycle) {
mFragmentManager = fragmentManager;
mLifecycle = lifecycle;
super.setHasStableIds(true);
}
帮我完成这个方法到return当前LifeCycle
private fun getCurrentLifeCycle(): LifeCycle { return ??? }
只需将 getLifecycle()
称为 returns 一个 LifeCycle
对象。
查看更多:https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html#getLifecycle()
我对请求 LifeCycle 的 FragmentStateAdapter
有疑问,请帮助我如何在下面的代码行中设置和传递 LifeCycle。
viewPager2.adapter = myViewPagerAdapter(supportFragmentManager, getCurrentLifeCycle())
这里是myViewPagerAdapter
方法
private fun myViewPagerAdapter(fm: FragmentManager, lifeCycle: Lifecycle): RecyclerView.Adapter<*> {
//val items = items // avoids resolving the ViewModel multiple times
return object : FragmentStateAdapter(fm, lifeCycle) {
override fun getItemCount(): Int {
// Show fragments.size total pages.
return fragments.size
}
private val fragments = arrayOf(
MyPostsFragment(),
RecentPostsFragment()
)
override fun createFragment(position: Int): Fragment {
return fragments[position]
}
}
}
经过一番挖掘,我明白了,LifeCycle 是 @NonNull
public FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
@NonNull Lifecycle lifecycle) {
mFragmentManager = fragmentManager;
mLifecycle = lifecycle;
super.setHasStableIds(true);
}
帮我完成这个方法到return当前LifeCycle
private fun getCurrentLifeCycle(): LifeCycle { return ??? }
只需将 getLifecycle()
称为 returns 一个 LifeCycle
对象。
查看更多:https://developer.android.com/reference/android/arch/lifecycle/LifecycleOwner.html#getLifecycle()