[英]call next fragment's lifecycle method when use viewpager2 with TabLayoutMediator
I use Viewpager2 With setup tablayout using TabLayoutMediator but the issue is when I swipe viewpager first fragment to the second fragment here only call current fragment's lifecycle methods, but when I click on tablayout then call next fragment's lifecycle methods.我使用 Viewpager2 和设置 tablayout 使用 TabLayoutMediator 但问题是当我将 viewpager 第一个片段刷到第二个片段时,这里只调用当前片段的生命周期方法,但是当我点击 tablayout 时,然后调用下一个片段的生命周期方法。 I don't know why this happens?我不知道为什么会这样?
This happens because the method that TabLayoutMediator uses to move to the selected fragment causes the RecyclerView to cache the next Fragment as it uses Smooth Scrolling.发生这种情况是因为 TabLayoutMediator 用于移动到选定片段的方法导致 RecyclerView 在使用平滑滚动时缓存下一个片段。
This should not be a problem UNLESS you are doing something in the wrong part of the Fragment's lifecycle.这应该不是问题,除非您在 Fragment 生命周期的错误部分做某事。 If you only want do something when a Fragment is displayed then only do it in the Fragments onResume
method as the behaviour you describe will only take the cached Fragment to "Started" state.如果您只想在显示 Fragment 时执行某些操作,则只能在 Fragments onResume
方法中执行此操作,因为您描述的行为只会将缓存的 Fragment 置于“已启动”状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.