简体   繁体   中英

Menu Item onClickListener for fragments

I have the following menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    app:showAsAction="never"
    android:title="Settings" />

<item android:id="@+id/menu_share"
    android:icon="@drawable/ic_heart_outline_white_24dp"
    app:showAsAction="always"
    android:title="Favorite" />
</menu>

How would I set an onClick listener for this? bearing in mind that my class extends Fragment:

public class My_Profile extends Fragment implements ....{}

...and I do not know if this makes any difference but I have already inflated a layout in this java file too.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

myView = inflater.inflate(R.layout.testFragment, container, false);
...
}

Update 1

This is what I tried after the suggestions but nothing seems to happen can someone please tell me what im doing wrong:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }


 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.menu_share) {
            Log.d("ButtonClick", "button click worked");
        }

        return super.onOptionsItemSelected(item);
    }

and in the onCreate added this:

setHasOptionsMenu(true);

Update 2

Dont know if this makes any difference, I'm just working with an example at the moment and the menu is also inflated like this. I dont know if that affects the other 2 methods:

 mToolbar.inflateMenu(R.menu.menu_main);

update 3

This is now what I have tried yet, still the button click doesn't log anything:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable  Bundle savedInstanceState) {

        setHasOptionsMenu(true);

        myView = inflater.inflate(R.layout.testFragment, container, false);

        ...

        return myView;
        }

and just for testing redirected to one of my other Activities, yet nothing still happens:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.menu_share) {
            Intent myIntent = new Intent(getActivity(),LoginActivity.class);
            startActivity(myIntent);
        }else{
            Intent myIntent = new Intent(getActivity(),SignupActivity.class);
            startActivity(myIntent);
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_main, menu);
    }

Your fragment should have these two methods to handle the menu: onCreateOptionsMenu and onOptionsItemSelected with the onClick stuff in the latter.

For example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        //TODO
    } else if (id == R.id.menu_share) {
        //TODO
    }

    return super.onOptionsItemSelected(item);
}

EDIT

Don't forget setHasOptionsMenu(true) for your fragment (thanks)

Well, I think that you have a good code. All you need to do is re-arrange the onCreateOptionsMenu(...)

For instance:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    return inflater.inflate(R.layout.testFragment, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_main, menu);
}

I hope this helps.

@BadCodersHub i have tried your scenario and its working fine with following code.

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    View myView = inflater.inflate(R.layout.fragment_main, container, false);
    return myView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu,inflater);
    inflater.inflate(R.menu.menu_main, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_settings:
            Log.d("","action Settings Clicked !!");
            break;
        case R.id.menu_share:
            Log.d("","action menu_share Clicked !!");
            break;
        default:
            return super.onOptionsItemSelected(item);
    }

    return super.onOptionsItemSelected(item);
}

Hope this will help you.you need to override onCreateOptionMenu and onOptionItemSelected methods

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