我正在使用 Sherlock Library 的 ActionBar 开发一个应用程序。我想在 ActionBar 的右上角添加一个项目列表,即 ActionBar 中的ListNavigation。
目前我正在使用以下代码:
public class MainActivity extends SherlockFragmentActivity {
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_TABS);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
this,
R.array.locations,
android.R.layout.simple_spinner_dropdown_item);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
/** 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);
tab = mActionBar.newTab()
.setText("Apple-2")
.setIcon(R.drawable.apple)
.setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab()
.setText("Apple-3")
.setIcon(R.drawable.apple)
.setTabListener(tabListener);
mActionBar.addTab(tab);
}
}
1)但我在mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
上下文中遇到错误,这在这里不起作用。
2)我不知道如何在单击此按钮时弹出列表。
3)另外,当我为导航列表实现这三行代码时
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(
this,
R.array.locations,
android.R.layout.simple_spinner_dropdown_item);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
我的标签被隐藏了。请建议我帮助我。