J'ai créé une application qui utilise Fragment. Depuis ma MainActivity, j'ai défini l'ActionBar.
Mais dans l'un de mes fragments, je dois modifier les icônes d'action et cliquer.
Donc, avec le code ci-dessous, lorsque je charge mon fragment, il affiche toujours le menu de la barre d'action de MainActivity
voici ma MainActivity:
public void restoreActionBar() {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
// enable ActionBar app icon to behave as action to toggle nav drawer
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
//Handle the Search Menu
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
searchView.setQueryHint(this.getString(R.string.action_search));
((EditText)searchView.findViewById(Android.support.v7.appcompat.R.id.search_src_text))
.setHintTextColor(getResources().getColor(R.color.white));
searchView.setOnQueryTextListener(OnQuerySearchView);
mSearchCheck = false;
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}//end onCreateOptionsMenu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
/** CAMERA **/
case R.id.action_camera:
//openCamera();
Utils.makeToast(getApplicationContext(), "Implement Camera", false);
return true;
/** SEARCH **/
case R.id.action_search:
//openSearch();
mSearchCheck = true;
Utils.makeToast(getApplicationContext(), "Implement Search", false);
return true;
/** SETTINGS **/
case R.id.action_settings:
//openSettings();
Utils.makeToast(getApplicationContext(), "Implement Settings", false);
return true;
/** ABOUT **/
case R.id.action_help:
//openHelp();
Utils.makeToast(getApplicationContext(), "Implement Help", false);
return true;
default:
return super.onOptionsItemSelected(item);
}//end switch
}//end onOptionsItemSelected
private OnQueryTextListener OnQuerySearchView = new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String arg0) {
// TODO Auto-generated method stub
if (mSearchCheck){
// implement your search here
}
return false;
}
};//end OnQueryTextListener
Voici la mise en page:
<menu xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
tools:context="com.example.sellbeesclean.MainActivity" >
<!-- CAMERA -->
<item
Android:id="@+id/action_camera"
Android:orderInCategory="100"
Android:icon="@drawable/ic_action_camera"
Android:title="@string/action_camera"
app:showAsAction="ifRoom|collapseActionView"/>
<!-- SEARCH -->
<item
Android:id="@+id/action_search"
Android:orderInCategory="100"
Android:icon="@drawable/ic_action_search"
Android:title="@string/action_search"
app:actionViewClass="Android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<!-- SETTINGS -->
<item
Android:id="@+id/action_settings"
Android:orderInCategory="100"
Android:title="@string/action_settings"
app:showAsAction="never"/>
<!-- HELP -->
<item
Android:id="@+id/action_help"
Android:orderInCategory="100"
Android:icon="@drawable/ic_action_help"
Android:title="@string/action_help"
app:showAsAction="ifRoom|collapseActionView"/> </menu>
Elle est mon fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.user_profile_fragment, container, false);
Log.i(TAG, "onCreateView");
.....
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
/** EDIT **/
case R.id.action_edit:
//openEditProfile(); //Open Edit Profile Fragment
Utils.makeToast(MyApplication.getAppContext(), "Implement Camera", false);
return true;
default:
return super.onOptionsItemSelected(item);
}//end switch
}//end onOptionsItemSelected
ici la disposition du menu fragment
<menu xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
tools:context="com.example.sellbeesclean.MainActivity" >
<!-- EDIT -->
<item
Android:id="@+id/action_edit"
Android:orderInCategory="100"
Android:icon="@drawable/ic_action_edit"
Android:title="@string/action_edit_profile"
app:showAsAction="ifRoom|collapseActionView"/></menu>
Dans la méthode onCreateView de votre fragment, écrivez
setHasOptionsMenu(true);
Et gonflez votre fichier XML de menu dans la méthode onCreateOptionsMenu
Dans onCreateOptionsMenu d'un fragment, écrivez
menu.clear();
avant de gonfler les menus