Reputation: 201
I am implementing Custom Action Bar in my App. In a fragment i am also using Tab Navigation. For that i have a Parent Fragment in which i make web requests and five other fragments for Tab Navigation. I want to add different Action Button in Action Bar for Different Fragmnet and their listener accordingly. I have only one Activity which is inflating my custom Action Bar.
Here is the Code for My Activity.
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerListLeft;
private ListView mDrawerListRight;
private ActionBarDrawerToggle mDrawerToggle;
private ArrayList<NavDrawerItem> navDrawerItemsLeft;
private ArrayList<NavDrawerItem> navDrawerItemsRight;
private NavDrawerListAdapter adapterLeftDrawer;
private NavDrawerListAdapter adapterRightDrawer;
public static ActionBar myCustomActionBar;
ImageButton imgLeftMenu;
ImageButton imgRightMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater inflator=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.header, null);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#93b636")));
getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
getSupportActionBar().setCustomView(v);
myCustomActionBar = getSupportActionBar();
}
}
Here is the code for My Frgment Listner
public class Shops extends Fragment implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = new String[] {"Tab 1", "Tab 2", "Tab 3", "Tab 4"};
public Shops(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_shop, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
actionBar = MainActivity.myCustomActionBar;
mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
return rootView;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
And This is My Tab Fragment
public class ShopFragment extends Fragment {
public ShopFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.sort_button, menu);
}
}
My sort Button XML is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/edit_item"
android:icon="@drawable/icon_sort"
android:orderInCategory="10"
android:showAsAction="ifRoom"
android:title="@string/Shops">
</item>
</menu>
Any Help or Useful tutorial will be Appreciated.
Thanks
Upvotes: 3
Views: 7818
Reputation: 1754
You need to call setHasOptionsMenu(true);
in onCreate()
of your fragment. This will allow you to set different option menus on fragments. Setup your options menu from within onCreateOptionsMenu(Menu menu, MenuInflater inflater)
Moreover, To get your Action bar instance, write getSuppportActionBar()
and then make whatever changes you want to do.
For backwards compatibility it's better to place this call as late as possible at the end of onCreate()
or even later in onActivityCreated()
or something like that.
Hope this helps you.
Upvotes: 9