我使用的是viewpager选项卡+刷卡,我想在动作条每个片段设置不同的标题,这样,当我切换,标题的变化。
I am using a viewpager "tabs + swipe" and I would like to set different titles in the actionBar for each fragment so that when I switch, title changes.
我试了几件事情没有成功,只有最后一个标题显示...并不会再改变,当我转...
I tried several things without success, only the last title displays... and does not change anymore when I switch...
首先,让你的活动实施OnPageChangeListener.
First, make your activity implement an OnPageChangeListener
.
然后,当你创建你的 ViewPager
,你可以使用mViewPager.setOnPageChangeListener(this)让你的活动将接收回调时,页面的变化。
Then, when you create your ViewPager
, you can use mViewPager.setOnPageChangeListener(this)
so that your activity will receive callbacks when the page changes.
最后,你需要实现 OnPageChangeListener
回调。你的 onPageSelected()
方法应该是这样的:
Finally, you need to implement the OnPageChangeListener
callbacks. Your onPageSelected()
method should be something like this:
@Override
public abstract void onPageSelected(int position) {
setTitle(getTitleFromPosition(position));
}
另外两个回调可以是空的。
The other two callbacks can be empty.