简体   繁体   中英

Android How to edit the Action bar menu from Fragment

I created an App that uses Fragment. From my MainActivity I set the ActionBar.

But in one of my Fragment I need to modify the action icons and on click.

So With the code below, when I load the my Fragment, it still display the action bar menu from MainActivity

here is my 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

Here is the layout :

<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>

Her is my 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

here the fragment menu layout

<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>

In your fragment's onCreateView method write

setHasOptionsMenu(true);

And inflate your menu xml file in onCreateOptionsMenu method

In onCreateOptionsMenu of a fragment, write

menu.clear();

before inflating menus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM