How to know when fragment actually visible in viewpager(如何知道片段何时在 viewpager 中实际可见)
问题描述
我在 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 中实际可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何知道片段何时在 viewpager 中实际可见


基础教程推荐
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01