我在 ViewPager 中使用了 4 个片段,因为 ViewPager 会提前加载上一个和下一个片段,并且在片段之间导航时不会调用生命周期方法.那么有什么方法可以检测 Fragment 何时真正可见.提前致谢.
I am using 4 fragments inside a ViewPager ,as ViewPager load the previous and next fragment in advance ,and no lifecycle method is called when navigating between fragments. So is there any way to detect when Fragment is actually visible. Thanks in Advance.
当然.假设 viewPager
是您的 ViewPager
实例,请使用:viewPager.getCurrentItem()
.
Of course. Assuming that viewPager
is your instance of the ViewPager
, use: viewPager.getCurrentItem()
.
在您的 Fragment
中,您可以检查其实例是否对用户可见,如下所示:
Within your Fragment
you can check if its instance is visible to the user like so:
@Override
public void setUserVisibleHint(boolean visible) {
super.setUserVisibleHint(visible);
if (visible) {
Log.i("Tag", "Reload fragment");
}
}
在提出问题之前,请务必彻底搜索答案.例如,您应该首先检查的地方是:https:///developer.android.com/reference/android/support/v4/view/ViewPager.html
Always make sure that you search for answers throughly before asking your question. For instance, the first place you should check would be: https://developer.android.com/reference/android/support/v4/view/ViewPager.html
这篇关于如何知道片段何时在 viewpager 中实际可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!