如何知道片段何时在 viewpager 中实际可见

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)