如何获取最后选择的操作栏选项卡?
how to get last selected action bar tab?
如何获取上次选择的操作栏选项卡,并从中导航到下一个选项卡?
我试过的:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
title);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.SimpleOnPageChangeListener#onPageSelected(int)
*/
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < adapter.getCount(); i++) {
ActionBar.Tab tab = actionBar.newTab();
tab.setText(adapter.getPageTitle(i));
tab.setTabListener(this);
actionBar.addTab(tab);
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
// here How Can I get previous selected tab from which I am navigating
}
所以在 onTabSelected()
方法中,我如何知道我是从哪个选项卡导航到这个 Tab.Is 有什么办法吗?
您可以创建一个名为
的字段
int fLastTab = -1;
您可以在函数 onTabSelected 中更新此字段
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
// Here you can check the value of fLastTab,
// if fLastTab == -1 there wasn't any last selected tab
// and if it has another value you have the last selected tab
// at the end of this function you will update the value of fLastTab
// Doing somenthing with fLastTab....
fLastTab = tab.getPosition();
}
还有一个方法,可以覆盖函数
public void onTabUnselected(Tab tab, FragmentTransaction ft){
// Doing somenthing with the last selected action bar tab (tab.position())...
}
你有退出选中状态的选项卡的位置。 onTabUnselected
如何获取上次选择的操作栏选项卡,并从中导航到下一个选项卡?
我试过的:
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
title);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
/* (non-Javadoc)
* @see android.support.v4.view.ViewPager.SimpleOnPageChangeListener#onPageSelected(int)
*/
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < adapter.getCount(); i++) {
ActionBar.Tab tab = actionBar.newTab();
tab.setText(adapter.getPageTitle(i));
tab.setTabListener(this);
actionBar.addTab(tab);
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
// here How Can I get previous selected tab from which I am navigating
}
所以在 onTabSelected()
方法中,我如何知道我是从哪个选项卡导航到这个 Tab.Is 有什么办法吗?
您可以创建一个名为
的字段int fLastTab = -1;
您可以在函数 onTabSelected 中更新此字段
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
// Here you can check the value of fLastTab,
// if fLastTab == -1 there wasn't any last selected tab
// and if it has another value you have the last selected tab
// at the end of this function you will update the value of fLastTab
// Doing somenthing with fLastTab....
fLastTab = tab.getPosition();
}
还有一个方法,可以覆盖函数
public void onTabUnselected(Tab tab, FragmentTransaction ft){
// Doing somenthing with the last selected action bar tab (tab.position())...
}
你有退出选中状态的选项卡的位置。 onTabUnselected