确定片段何时在 ViewPager2 中不再可见
Determining when fragment no longer visible in ViewPager2
我希望从 ViewPager
迁移到 ViewPager2
。我现有的 ViewPager
包含支持通过 ActionMode
进行批量编辑的 ListFragment
。当用户从一个页面滑动到另一个页面时,我需要取消当前的批量编辑操作,以便在查看不同页面时不会显示一个页面的上下文 header。
使用 ViewPager
,我可以覆盖 ListFragment
的 FragmentPagerAdapter
中的 setPrimaryItem
以了解一页何时替换另一页。至关重要的是,此函数将变得可见的实际 child 片段作为参数,我可以将其存储在适配器中,稍后在它被另一个片段替换时用于取消批量编辑。
如何对 ViewPager2
做同样的事情,即知道片段何时不再可见?我已经尝试覆盖 Fragment.onPause
,但这不是我想要的——只要 phone 屏幕关闭或方向改变,它就会被调用,我不想在这些情况下取消批量编辑.我也有一个通过 ViewPager2.registerOnPageChangeCallback
注册的回调,但这只给我新片段的索引,而不是片段本身或任何关于旧片段的信息。
我认为最好的解决办法是注册一个FragmentTransactionCallback
。缺点 - 这仅在 ViewPager2 库的 the latest 1.1.0 alpha release 中添加。
class (FragmentTransactionCallback::onFragmentMaxLifecyclePreUpdated
) 上有一个方法,每当 ViewPager2
发生变化时调用 a Fragment's maximum Lifecycle state。
val basicAdapter: FragmentStateAdapter = // ....
basicAdapter.registerFragmentTransactionCallback(object : FragmentTransactionCallback() {
override fun onFragmentMaxLifecyclePreUpdated(fragment: Fragment, max: Lifecycle.State): OnPostEventListener {
// Check whether a Fragment is going to move from 'resumed' to 'started'
//
// Note that this check won't catch some types of smooth scroll:
// I'm not certain why, but I think it has to do with fragments
// already being paused before the method is called.
//
if (max == Lifecycle.State.STARTED && fragment.isResumed) {
// This fragment WAS the 'current item' but it's offscreen now.
return OnPostEventListener { TODO("Cancel Bulk Editing") }
}
return super.onFragmentMaxLifecyclePreUpdated(fragment, max)
}
})
我希望从 ViewPager
迁移到 ViewPager2
。我现有的 ViewPager
包含支持通过 ActionMode
进行批量编辑的 ListFragment
。当用户从一个页面滑动到另一个页面时,我需要取消当前的批量编辑操作,以便在查看不同页面时不会显示一个页面的上下文 header。
使用 ViewPager
,我可以覆盖 ListFragment
的 FragmentPagerAdapter
中的 setPrimaryItem
以了解一页何时替换另一页。至关重要的是,此函数将变得可见的实际 child 片段作为参数,我可以将其存储在适配器中,稍后在它被另一个片段替换时用于取消批量编辑。
如何对 ViewPager2
做同样的事情,即知道片段何时不再可见?我已经尝试覆盖 Fragment.onPause
,但这不是我想要的——只要 phone 屏幕关闭或方向改变,它就会被调用,我不想在这些情况下取消批量编辑.我也有一个通过 ViewPager2.registerOnPageChangeCallback
注册的回调,但这只给我新片段的索引,而不是片段本身或任何关于旧片段的信息。
我认为最好的解决办法是注册一个FragmentTransactionCallback
。缺点 - 这仅在 ViewPager2 库的 the latest 1.1.0 alpha release 中添加。
class (FragmentTransactionCallback::onFragmentMaxLifecyclePreUpdated
) 上有一个方法,每当 ViewPager2
发生变化时调用 a Fragment's maximum Lifecycle state。
val basicAdapter: FragmentStateAdapter = // ....
basicAdapter.registerFragmentTransactionCallback(object : FragmentTransactionCallback() {
override fun onFragmentMaxLifecyclePreUpdated(fragment: Fragment, max: Lifecycle.State): OnPostEventListener {
// Check whether a Fragment is going to move from 'resumed' to 'started'
//
// Note that this check won't catch some types of smooth scroll:
// I'm not certain why, but I think it has to do with fragments
// already being paused before the method is called.
//
if (max == Lifecycle.State.STARTED && fragment.isResumed) {
// This fragment WAS the 'current item' but it's offscreen now.
return OnPostEventListener { TODO("Cancel Bulk Editing") }
}
return super.onFragmentMaxLifecyclePreUpdated(fragment, max)
}
})