我正在使用使用 Sherlock 库的 ActionBar。在这个应用程序中也实现了选项卡。
现在,当我在 ActionBar 中使用 NavigationList 时,我丢失了 Tab 片段。为了在我使用的 ActionBar 中使用 ListNavigation:
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
&在此之前,我mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
用于选项卡但没有列表。
现在,我想同时使用 ,NavigationList
和Tab Fragments
public class MainActivity extends SherlockFragmentActivity implements OnNavigationListener {
ActionBar mActionBar;
ViewPager mPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to action bar of this activity */
mActionBar = getSupportActionBar();
/** Set tab navigation mode */
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
//mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
this,
R.array.locations,
android.R.layout.simple_spinner_item);
mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
mActionBar.setIcon(R.drawable.app_icon);
/** Getting a reference to ViewPager from the layout */
mPager = (ViewPager) findViewById(R.id.pager);
/** Getting a reference to FragmentManager */
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionBar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listner to the viewPager */
mPager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the FragmentPagerAdapter object to the viewPager object */
mPager.setAdapter(fragmentPagerAdapter);
mActionBar.setDisplayShowTitleEnabled(true);
/** Defining tab listener */
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
/** Creating Android Tab */
Tab tab = mActionBar.newTab()
.setText("Android")
.setIcon(R.drawable.android)
.setTabListener(tabListener);
mActionBar.addTab(tab);
/** Creating Apple Tab */
tab = mActionBar.newTab()
.setText("Apple")
.setIcon(R.drawable.apple)
.setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
MyFragmentPagerAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
final int PAGE_COUNT = 2;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
Bundle data = new Bundle();
switch(arg0){
/** Android tab is selected */
case 0:
AndroidFragment androidFragment = new AndroidFragment();
data.putInt("current_page", arg0+1);
androidFragment.setArguments(data);
return androidFragment;
/** Apple tab is selected */
case 1:
AppleFragment appleFragment = new AppleFragment();
data.putInt("current_page", arg0+1);
appleFragment.setArguments(data);
return appleFragment;
}
return null;
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
}
和其他两个类:1)public class AppleFragment extends SherlockListFragment
2)public class AndroidFragment extends SherlockListFragment
请建议我,为什么我在将列表添加到 ActionBar 时会丢失我的选项卡。