问题描述
在我的申请,我有一个ViewPager持有,里面许多片段可滑动展示标签。我用的是 setUserVisibleHint
方法时,片段
说到屏幕检测。这个伟大的工程,当用户标签之间挥笔,但它不会对第一负荷工作。在我向左滑动,然后再回到第一个片段的方法,运行code
因为 setUserVisibleHint
方法是在 onCreateView
方法之前调用。
In my application, I have a ViewPager which holds many swipeable Tabs with Fragments inside. I use the setUserVisibleHint
method to detect when a Fragment
comes to the screen. This works great when the user swipes between tabs but it does not work on the first load. To run the code in the method I have to swipe to left and then back to the first Fragment
because the setUserVisibleHint
method is called before the onCreateView
method.
你有后的第一个片段是可见我如何运行此code什么想法?有没有一种方法在 ViewPager
或其他什么东西?
Do you have any ideas how I can run this code after the first Fragment is visible? Is there a method in the ViewPager
or something else?
推荐答案
您不能(也不应该)依靠 setUserVisibleHint
这一点。相反,你应该使用ViewPager.OnPageChangeListener得到回调时,页面变得可见。例如。
You can't (and shouldn't) rely on setUserVisibleHint
for this. Instead, you should be using a ViewPager.OnPageChangeListener to get callbacks for when a page becomes visible. E.g.
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// do your work
}
});
的注意的:你可以使用 ViewPager.SimpleOnPageChangeListener
如果您不需要监听所有的回调
Note: You can use ViewPager.SimpleOnPageChangeListener
if you don't need to listen for all callbacks.
这篇关于ViewPager调用setUserVisibleHint后第一个片段是加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!