How to set action bar and left drawer attributes for fragments according to their types
I have different types of fragments in my application and there are 3 icons on the ActionBar (filter, update and sort), but I don't want to show all 3 icons in each of the fragments. I only have to show some of them according to the snippet.
Similar thing I want to do with the left drawer. In some snippets I want to show the left box, while I don't want to display the left box on others.
I have an Activity class in my application where I am attaching these fragments and I am currently handling these two things in this class and the code has gotten messy with if-else conditions.
So right now I'm checking the snippet name and then setting the action bar icons and left box attributes to match it.
Please tell me the best way to do this (preferably to deal with this in the snippet itself)
thank
source to share
Fragments have access to their activity through the getActivity () function , which will return a non- null activity after onAttach () is called (and before onDetach () ). Once a fragment has an activity, it can say that it does whatever you do right in the activity, manually with checks, including changing the action bar buttons.
source to share
To show the options depending on the snippet, you can simply do the following:
Add setHasOptionsMenu(true)
a onCreate()
fragment to the method and tell Actions to redraw its options menu.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().invalidateOptionsMenu();
}
Next, override the method onCreateOptionsMenu()
to inflate the options you want for your snippet.
// No support library - support library api slightly different
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Add Fragment menu elements to Activity menu elements
inflater.inflate(R.menu.myfragmentmenu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
Finally, be sure to write down all the option items in onOptionsItemSelected()
your activity method . (Important note: be sure to replace fragments instead of adding them, otherwise it onCreateOptionsMenu()
will be called for each fragment.)
To disable and enable a drawer, you can add the following way to your activity and call it from your fragment:
public void toggleDrawer(boolean enabled) {
if (enabled) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerToggle.setDrawerIndicatorEnabled(true);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
}
}
source to share
Create a new project from the ActionBarCombat sample project and also download the sample app here http://www.learn2crack.com/2014/06/android-sliding-navigation-drawer-example.html
I once combined the two to come up with an app that has action bar attributes as well as a left drawer
source to share
To structure the code a bit, why don't you create some methods in your activity, such as displayRefreshIcon (boolean visible), in which you handle the visibility of these elements.
From your snippet, you can call these methods (like frangulyan) through the getActivity () function. If (getActivity ()! = Null && getActivity () instanceof MyActivity) {((MyActivity) getActivity ()) displayRefreshIcon (true). }
source to share
In the main thread or normal case, it is somehow impossible to make changes in the activity itself, because fragments are a split module that is associated with activities, but not part of them. But there is a shortcut that is to send a message (handler) to the activity to update the display of the corresponding components of the action bar (most likely if you are only using this snippet for a specific activity).
source to share
When using any type of fragment, you should simply have access to the methods (you should override them): onCreateOptionsMenu, onPrepareOptionsMenu, and onOptionsItemSelected. These methods should provide you with a lot of grips for creating the menu per slice. You can create a menu layout file per snippet and process them in the method intended for this. Methods:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.overviewmenu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
source to share