Reputation: 2519
I am working on an application, using ActionBar of Sherlock Library. I want to add a List of items at the Right corner of the ActionBar, that is, ListNavigation in ActionBar.
Currently I am using the following code:
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) but I am getting error in mActionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
context this is not working here.
2)I am not getting how to popup the list on this button click.
3)Also, when i implemented these three line of code for Navigation List
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);
My Tabs got hidden. Please suggest me help me.
Upvotes: 0
Views: 392
Reputation: 8543
1) Does your activity implement OnNavigationListener? It doesn't seem to be, and you're passing a reference to your Activity instance to setListNavigationCallbacks() which needs an OnNavigationListener.
2) I'm not sure what you mean by this button click.
3) Your tabs got hidden because you're specifying NAVIGATION_MODE_LIST as opposed to NAVIGATION_MODE_TABS. You can only have one or the other as part of the ActionBar.
What you could do if you want both tabs and drop-down is use NAVIGATION_MODE_LIST in the ActionBar, then have the Fragment within your activity contain a set of tabs. Have a look at the TabHost class.
Upvotes: 1