如何知道片段何时在ViewPager中实际可见 [英] How to know when fragment actually visible in viewpager

查看:114
本文介绍了如何知道片段何时在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.

推荐答案

当然可以.假设viewPagerViewPager的实例,请使用: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中实际可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆