将ViewPager中的片段设置为当前页面时的回调 [英] Callbacks for when a Fragment in a ViewPager is set as current page
问题描述
我有一个viewpager,它有5个片段页面.我可以通过点击导航按钮在每个片段之间进行切换.
I have a viewpager, it has 5 fragments page. I can switch between each fragment by tapping a navigation button.
现在,我要实现的是进入页面时刷新页面1.(例如,我在第1页中,当我点击第2页的按钮时,第2页将显示并刷新.
Now, what I'm trying to achieve is to refresh 1 of the page when entering it. (e.g. I'm in Page 1, when I tap button for Page 2, Page 2 will display and refresh.
但是,我发现此行为未调用片段中的onResume方法.或其他任何生命周期方法.
However, I find out that the onResume method in the fragment is not called on this behavior. or any of the other lifecycle method.
我似乎找不到办法.在这种情况下我可以使用任何回调吗?
I can't seem to find a way to do this. Is there any callbacks I can use for such scenario?
推荐答案
由于不需要任何缓存,因此使用 UPD::谢谢,Yarh,指出了 viewPager.setOffscreenPageLimit(0)
,这样就不会在后台加载页面,并且仅在实际显示页面时才会调用 onResume
. setOffscreenPageLimit(0)
不起作用.抱歉.
Since you don't need any kind of caching, it's probably a good idea for you to use
UPD: Thanks, Yarh, for pointing out that viewPager.setOffscreenPageLimit(0)
so pages won't be loaded in background, and onResume
will be called only when page is actually displayed.setOffscreenPageLimit(0)
doesn't work. Sorry for that.
如果这不适用于您,请使用 viewPager.addOnPageChangeListener(listener)
- onPageSelected(position)
方法应该有帮助.
If this doesn't work for you, use viewPager.addOnPageChangeListener(listener)
- onPageSelected(position)
method should help.
这篇关于将ViewPager中的片段设置为当前页面时的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!