我有一项活动实施ViewPager
。它有两个碎片。我想在ActionItems
上添加ActionBar
根据当前哪个片段处于活动状态。我没有在Activity上添加菜单项。这是我的两个片段的代码。
对于片段A:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_one, menu);
MenuItem searchItem = menu.findItem(R.id.searchWidgetOne);
SearchView search = new SearchView(getActivity());
search.setOnQueryTextListener(this);
searchItem.setActionView(search);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_items_one:
Intent addCrInent = new Intent(getActivity(), ActivityA.class);
startActivity(addCrInent);
return true;
case R.id.action_items_two:
Intent addStIntent = new Intent(getActivity(), ActivityB.class);
startActivity(addStIntent);
return true;
default:
return false;
}
}
对于片段B:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_second, menu);
MenuItem searchItem = menu.findItem(R.id.searchWidgetTwo);
SearchView search = new SearchView(getActivity());
search.setOnQueryTextListener(this);
searchItem.setActionView(search);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.action_items_one_secondMenu:
Intent addCrInent = new Intent(getActivity(), ActivityA.class);
startActivity(addCrInent);
return true;
case R.id.action_items_two_secondMenu:
Intent addStIntent = new Intent(getActivity(), ActivityB.class);
startActivity(addStIntent);
return true;
default:
return false;
}
}
对于两个碎片,在MY onCreate中,我写了这一行:
setHasOptionsMenu(true);
这是我的menu_one.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/searchWidgetOne"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
android:title="@string/action_search"/>
<item
android:id="@+id/action_items_one"
android:icon="@drawable/ic_action_new_event"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="@string/add_course"/>
<item
android:id="@+id/action_items_two"
android:icon="@drawable/ic_action_add_person"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="@string/add_student"/>
这是我的menu_second.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/searchWidgetTwo"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always|collapseActionView"
android:title="@string/action_search"/>
<item
android:id="@+id/action_items_one_secondMenu"
android:icon="@drawable/ic_action_new_event"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="@string/add_course"/>
<item
android:id="@+id/action_items_two_secondMenu"
android:icon="@drawable/ic_action_add_person"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="@string/add_student"/>