onCreateOptionsMenu wewnątrz fragmentów


182

Umieściłem w setHasOptionsMenu(true)środku onCreateView, ale nadal nie mogę wywoływać onCreateOptionsMenufragmentów.

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

Poniżej znajduje się mój onCreateOptionsMenukod.

@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
    getSupportMenuInflater().inflate(R.menu.layout, menu);
    return (super.onCreateOptionsMenu(menu));
}

Otrzymuję komunikat o błędzie:

Metoda onCreateOptionsMenu(Menu)typu Fragment musi zastąpić lub zaimplementować metodę nadtypu.


1
Dzięki za 'setHasOptionsMenu (true);', szukałem tego.
Fabricio PH,

Odpowiedzi:


497

Spróbuj tego,

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

I onCreatedodaj ten wiersz, aby opcje pojawiły się w twoimToolbar

setHasOptionsMenu(true);

182
nie zostanie wywołany, jeśli nie dodasz tego wiersza:setHasOptionsMenu(true);
Yoann Hercouet,

10
Funkcja onCreateOptionsMenu () dla fragmentów ma inne argumenty niż dla działań.
Jorge,

3
a także inny typ zwracany jako void, w przeciwieństwie do boolean funkcji onCreateOptionsMenu () w działaniu, jak wspomniano powyżej.
Dexter

4
Interesująca uwaga: jeśli przesłonisz również opcję onCreateOptionsMenu w zawierającym działanie, zostaną wyświetlone obie pozycje menu opcji.
Adam Johns

5
setHasOptionsMenu(true);musi być wezwany, onCreate()aby zakończyć.
Roel

21

Masz już automatycznie wygenerowany plik res / menu / menu.xml definiujący ustawienia działania .

W pliku MainActivity.java dostępne są następujące metody:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_settings:
            // do stuff, like showing settings fragment
            return true;
    }

    return super.onOptionsItemSelected(item); // important line
}

W onCreateView()metodzie wywołania fragmentu:

setHasOptionsMenu(true); 

a także dodaj te 2 metody:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_1:
            // do stuff
            return true;

        case R.id.action_2:
            // do more stuff
            return true;
    }

    return false;
}

Na koniec dodaj nowy plik res / menu / fragment_menu.xml definiujący działanie_1 i działanie_2 .

W ten sposób, gdy aplikacja wyświetli Fragment, jego menu będzie zawierać 3 wpisy:

  • akcja_1 z res / menu / fragment_menu.xml
  • akcja_2 z res / menu / fragment_menu.xml
  • action_settings z res / menu / menu.xml

1
pytanie dotyczyło fragmentów, a nie aktywności
OlivierM

2
@OlivierM odpowiedź wyjaśnia menu fragmentów. Doceniam wyjaśnienie, w jaki sposób współdziała z menu czynności.
Aranda

13

Próbowałem odpowiedzi na @Alexander Farber i @Sino Raj. Obie odpowiedzi są dobre, ale nie mogłem użyć onCreateOptionsMenu w moim fragmencie, dopóki nie odkryję tego, czego brakowało:

Dodaj setSupportActionBar (pasek narzędzi) do mojej działalności, w następujący sposób:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.id.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

Mam nadzieję, że ta odpowiedź może być pomocna dla kogoś z tym samym problemem.


4

Połączenie

setSupportActionBar(toolbar)

wewnątrz

onViewCreated(...) 

fragmentu

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    ((MainActivity)getActivity()).setSupportActionBar(toolbar);
    setHasOptionsMenu(true);
}

1
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_add_customer, container, false);
        setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_sample, menu);
    super.onCreateOptionsMenu(menu,inflater);
}
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.