Reputation: 1
I am trying to bind the ActionBar Tabs
to a ViewPager
. At the very beginning I created two separate projects for ActionBar Tabs
and ViewPager
, and they were working fine. When trying to bind the to each other, according to the code below, the ViewPager
comply to the TabListener
, in other sense, when I touch an ActionBar Tab
the ViewPager
change accordingly and display the respective View
.For an example, I have three Tabs
, when touching the Tab
number two, the ViewPager
displays the respective page number two. and so on.
But the ActionBar Tabs
do not obey the ViewPager
, in other sense, when swiping the screen to move to the next page of the ViewPager
, the ViewPager
shows the respective View
BUT the ActionBar Tab
does not change its current selected state according to the current ViewPager
's selected View
. For an example, when swiping to to the ViewPage
number three, the ViewPager
displays its respected View
which is number three, BUT, the current selected ActionBar Tab
does not change to be number three. I could be accessing ViewPage
three while the highlighted Tab
is number one.
I hope I explained the problem clearly.
MainActivity
private ViewPager mViewPager;
private MyTabsPagerAdapter mPagerAdapter;
private ActionBar mActionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Fragment> mFragList = new ArrayList<Fragment>();
mFragList.add(new Fragment01());
mFragList.add(new Fragment02());
mFragList.add(new Fragment03());
mViewPager = (ViewPager) findViewById(R.id.pager);
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPagerAdapter = new MyTabsPagerAdapter(getSupportFragmentManager(), mFragList);
mViewPager.setAdapter(mPagerAdapter);
for(int i=0; i<mFragList.size(); i++) {
mActionBar.addTab(mActionBar.newTab().setText("Fragment0"+(i+1)).setTabListener(this));
}
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
mViewPager.setCurrentItem(arg0.getPosition());
//mActionBar.setSelectedNavigationItem(arg0.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
MyTabsPagerAdapter
public class MyTabsPagerAdapter extends FragmentPagerAdapter {
List<Fragment> mFragList;
public MyTabsPagerAdapter(FragmentManager fm, List<Fragment> mFragList) {
super(fm);
// TODO Auto-generated constructor stub
this.mFragList = mFragList;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return this.mFragList.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return this.mFragList.size();
}
}
Upvotes: 1
Views: 776
Reputation: 1262
You need to set a listener for when the viewpager changes for example,
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
This waits for the viewpager to change and when it does, it sets the actionbar position equal to the position you moved the viewpager to.
Upvotes: 4