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...
First, make your activity implement an OnPageChangeListener
.
Then, when you create your ViewPager
, you can use mViewPager.setOnPageChangeListener(this)
so that your activity will receive callbacks when the page changes.
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.