我有一个主要活动来控制3个碎片。 MainActivity在选项菜单上有一个搜索栏和一些过滤器。问题是其中一个片段(ResultadosFragment)有自己的OnCreateOptionsMenu()实现,它会扩展自己的菜单。会发生什么 - 两个选项 - MainActivity和Fragment - 菜单只在一个选项菜单中混合在一起。
这是一个截图:
MainActivity - OnCreateOptionsMenu()
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
super.onCreateOptionsMenu(menu);
mOptionsMenu = menu;
//Carrega o arquivo de menu.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search_bar, menu);
menu.findItem(R.id.usado).setChecked(true);
menu.findItem(R.id.longplay).setChecked(true);
menu.findItem(R.id.nacional).setChecked(true);
//Pega o Componente.
final SearchView mSearchView = (SearchView) menu.findItem(R.id.search)
.getActionView();
//Define um texto de ajuda:
mSearchView.setQueryHint("Banda - Álbum");
// exemplos de utilização:
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
searchQueryGlobal = query;
realizarPesquisa();
mSearchView.setIconified(true);
mSearchView.clearFocus();
// call your request, do some stuff..
// collapse the action view
if (menu != null) {
(menu.findItem(R.id.search)).collapseActionView();
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.i("well", " this worked");
return false;
}
});
return true;
}
menu_search_bar.XML
<?xml version="1.0" encoding="utf-8"?>
<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.marcelofb.cratediggersguide.MainActivity">
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<!-- <item
android:id="@+id/searchOptions"
android:title="Tools"
android:icon="@android:drawable/ic_menu_manage"
app:showAsAction="always" /> -->
<item android:title="Menu No. 2">
<group android:id="@+id/radioGroupNovoUsado" android:checkableBehavior="single">
<item
android:id="@+id/novo"
android:title="NOVO"
app:showAsAction="never" >
</item>
<item
android:id="@+id/usado"
android:title="USADO"
app:showAsAction="never" />
</group>
</item>
<item android:id="@+id/radioGroupCompactoLPItem"
android:title="Menu No. 2">
<group android:id="@+id/radioGroupCompactoLP" android:checkableBehavior="single">
<item
android:id="@+id/compacto"
android:title="COMPACTO"
app:showAsAction="never" />
<item
android:id="@+id/longplay"
android:title="LP"
app:showAsAction="never" />
</group>
</item>
<item android:id="@+id/radioGroupNacionalImportadoItem"
android:title="Menu No. 2">
<group android:id="@+id/radioGroupNacionalImportado" android:checkableBehavior="single">
<item
android:id="@+id/nacional"
android:title="NACIONAL"
app:showAsAction="never" />
<item
android:id="@+id/importado"
android:title="IMPORTADO"
app:showAsAction="never" />
</group>
</item>
ResultadosFragment - OnCrateOptionsMenu()AND onOptionsItemSelected()
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_resultados, menu);
super.onCreateOptionsMenu(menu, inflater);
mOptionsMenu = menu;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuRemoveLowReputation:
exibirMensagemReputacaoMaxima();
return true;
/**case R.id.menuSortClosest:
exibirMensagemEdt();
return true;**/
case R.id.menuSortClosestGps:
trimmarAteCoeficienteQuinze();
return true;
case R.id.menuDeleteMaiorQue:
exibirMensagemDeletarMaiorQue();
return true;
case R.id.menuInserirCep:
exibirMensagemInserirCep();
return true;
case R.id.menuSortPrecoMenorNormalPacTabelado:
sortCheaperNormalPacTabelado();
mAdapter.clear();
mAdapter.addAll(recordsGlobal);
mAdapter.notifyDataSetChanged();
mAdapter.notifyDataSetInvalidated();
return true;
case R.id.menuSortPrecoMenorExpressoSedex:
sortCheaperExpressoSedexTabelado();
mAdapter.clear();
mAdapter.addAll(recordsGlobal);
mAdapter.notifyDataSetChanged();
mAdapter.notifyDataSetInvalidated();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_resultados.XML
<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"
app:showAsAction="ifRoom"
tools:context="com.example.marcelofb.cratediggersguide.MainActivity">
<item
android:id="@+id/menuRemoveLowReputation"
android:title="Remover reputação baixa" />
<!-- <item
android:id="@+id/menuSortClosest"
android:title="Mais Próximos - CEP" /> -->
<item
android:id="@+id/menuSortClosestGps"
android:title="C.V menor 15" />
<item
android:id="@+id/menuDeleteMaiorQue"
android:title="Deletar maior que" />
<item
android:id="@+id/menuInserirCep"
android:title="Inserir novo CEP para Frete" />
<item
android:id="@+id/menuSortPrecoMenorNormalPacTabelado"
android:title="Listar por preço menor Normal/Pac/Tabelado" />
<item
android:id="@+id/menuSortPrecoMenorExpressoSedex"
android:title="Listar por preço menor Expresso/Sedex" />
答案 0 :(得分:1)
您可以在menu.clear()
OnCreateOptionsMenu()
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);
}
我不知道这是不是最好的做法。如果有人知道告诉我们。 =)
答案 1 :(得分:0)
这些变化解决了这个问题!
<强> menu_search_bar.XML 强>
<?xml version="1.0" encoding="utf-8"?>
<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.marcelofb.cratediggersguide.MainActivity">
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item
android:id="@+id/searchOptions"
android:icon="@android:drawable/ic_menu_manage"
android:title="Tools"
app:showAsAction="always">
<menu>
<item android:title="Menu No. 2">
<group
android:id="@+id/radioGroupNovoUsado"
android:checkableBehavior="single">
<item
android:id="@+id/novo"
android:title="NOVO"
app:showAsAction="never"></item>
<item
android:id="@+id/usado"
android:title="USADO"
app:showAsAction="never" />
</group>
</item>
<item
android:id="@+id/radioGroupCompactoLPItem"
android:title="Menu No. 2">
<group
android:id="@+id/radioGroupCompactoLP"
android:checkableBehavior="single">
<item
android:id="@+id/compacto"
android:title="COMPACTO"
app:showAsAction="never" />
<item
android:id="@+id/longplay"
android:title="LP"
app:showAsAction="never" />
</group>
</item>
<item
android:id="@+id/radioGroupNacionalImportadoItem"
android:title="Menu No. 2">
<group
android:id="@+id/radioGroupNacionalImportado"
android:checkableBehavior="single">
<item
android:id="@+id/nacional"
android:title="NACIONAL"
app:showAsAction="never" />
<item
android:id="@+id/importado"
android:title="IMPORTADO"
app:showAsAction="never" />
</group>
</item>
</menu>
</item>
<强> menu_resultados.XML 强>
<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"
app:showAsAction="ifRoom"
tools:context="com.example.marcelofb.cratediggersguide.MainActivity">
<item
android:id="@+id/searchOptions"
android:icon="@drawable/ic_filter_list_white_24dp"
android:title="Tools"
app:showAsAction="always">
<menu>
<item
android:id="@+id/menuRemoveLowReputation"
android:title="Remover reputação baixa" />
<!-- <item
android:id="@+id/menuSortClosest"
android:title="Mais Próximos - CEP" /> -->
<item
android:id="@+id/menuSortClosestGps"
android:title="C.V menor 15" />
<item
android:id="@+id/menuDeleteMaiorQue"
android:title="Deletar maior que" />
<item
android:id="@+id/menuInserirCep"
android:title="Inserir novo CEP para Frete" />
<item
android:id="@+id/menuSortPrecoMenorNormalPacTabelado"
android:title="Listar por preço menor Normal/Pac/Tabelado" />
<item
android:id="@+id/menuSortPrecoMenorExpressoSedex"
android:title="Listar por preço menor Expresso/Sedex" />
</menu>
</item>