Android 检测片段何时可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21733355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect when fragment becomes visible
提问by Mdlc
Why not a duplicate:Suggested post only works for a viewpager, or when onResume is called.
为什么不重复:建议的帖子仅适用于 viewpager,或者当 onResume 被调用时。
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.
我有一个 listfragment 和一个 detailfragment。单击列表项时会打开详细信息片段。我通过隐藏 listfragment 并显示 detailfragment 来完成此操作。当用户返回时,他返回到列表片段。
How can I detect when the user gets back to my fragment, or when my fragment is visible?
如何检测用户何时返回到我的片段,或者我的片段何时可见?
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 whenthe fragment becomesvisible, and not a method to check ifit is visible.
请注意,我想用.hide()和show()和我正在寻找一个听众或onVisible检查,以保持当片段变得可见,而不是一个方法来检查,如果它是可见的。
回答by super-qua
EDIT:
As pointed out in the comments, setUserVisbileHint()
does not get called automatically, but by the FragmentPageAdapter when used in a ViewPager.
编辑:
正如评论中所指出的,setUserVisbileHint()
不会自动调用,而是在 ViewPager 中使用时由 FragmentPageAdapter 调用。
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.
对于问题中描述的场景,onHiddenChanged(boolean hidden)
http://developer.android.com/reference/android/app/Fragment.html#onHiddenChanged(boolean)是合适的。
如文档中所述,每次 Fragments 隐藏状态更改时都会调用该方法,但不会在 Fragment 的初始设置时调用。
You could use setUserVisibleHint()
http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)
你可以使用setUserVisibleHint()
http://developer.android.com/reference/android/support/v4/app/Fragment.html#setUserVisibleHint(boolean)
if isVisibleToUser
is set to true
the fragment should be displayed.
如果isVisibleToUser
设置为true
应该显示片段。
回答by Jorgesys
Read about the method : Fragment. isVisible()
阅读方法:片段。是可见的()
Example:
例子:
MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentById("myFragnmentID");
if (test != null && test.isVisible()) {
//VISIBLE! =)
}
else {
//NOT VISIBLE =(
}
回答by Snicolas
You could send an event to your activity when the fragment is started : https://www.inkling.com/read/programming-android-mednieks-1st/chapter-11/visualizing-the-fragment-life
您可以在片段启动时向您的活动发送事件:https: //www.inkling.com/read/programming-android-mednieks-1st/chapter-11/visualizing-the-fragment-life
By overriding the onStart method :
通过覆盖 onStart 方法:
@Override
public void onStart() {
super.onStart();
((MyInterface)getActivity()).onFragmentIsVisible( this );
}
Where MyInterface
is an interface :
MyInterface
接口在哪里:
public MyInterface {
public void onFragmentVisible( Fragment fragment );
}
and your activity will have to implement it.
你的活动将不得不实施它。
This could be more easy with an event bus library like
使用像这样的事件总线库可能会更容易