检测片段何时可​​见 [英] Detect when fragment becomes visible

查看:72
本文介绍了检测片段何时可​​见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不重复:建议的帖子仅适用于viewpager,或者在调用onResume时有效.

Why not a duplicate: Suggested post only works for a viewpager, or when onResume is called.

我有一个listfragment和一个detailfragment.单击列表项时,将打开详细信息片段.我通过隐藏listfragment完成此操作,并显示了detailfragment.当用户返回时,他返回到列表片段.

I have a listfragment and a detailfragment. The detail fragment is opened when a list item is clicked. I accomplish this by hiding the listfragment and I showing the detailfragment. When the user goes back, he returns to the listfragment.

如何检测用户何时返回我的片段,或何时可见我的片段?

How can I detect when the user gets back to my fragment, or when my fragment is visible?

请注意,我想继续使用.hide()和show(),并且我正在寻找一个监听器或onVisible来检查何时该片段成为可见,而不是检查是否可见的方法.

Please note that I would like to keep using .hide() and show() and that I am looking for a listener or a onVisible to check when the fragment becomes visible, and not a method to check if it is visible.

推荐答案


正如评论中指出的那样,setUserVisbileHint()不会自动调用,而是在ViewPager中使用时由FragmentPageAdapter调用.


As pointed out in the comments, setUserVisbileHint() does not get called automatically, but by the FragmentPageAdapter when used in a ViewPager.

对于问题中描述的场景,onHiddenChanged(boolean hidden) http://developer .android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean)是合适的.
如文档中所述,每次Fragments隐藏状态更改时都会调用该方法,但在Fragment的初始设置时不会调用该方法.

For the scenario described in the question, onHiddenChanged(boolean hidden) http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean) is suitable.
As stated in the documentation, the method gets called every time the Fragments hidden state changes, but not on the initial setup of the Fragment.

您可以使用setUserVisibleHint() http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)

如果isVisibleToUser设置为true,则应显示片段.

if isVisibleToUseris set to truethe fragment should be displayed.

这篇关于检测片段何时可​​见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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