第二个片段的ActionItems仅显示在ViewPager中的两个片段上

时间:2014-08-11 05:59:39

标签: android android-actionbar android-viewpager

我有ViewPager MainAvtivity创建的两个片段。我想添加ActionItemson ActionBar根据当前哪个Fragment是活动的。我没有在Activity上添加菜单项。但只有{{1>} 第二片段仅在两个片段上显示。 这是我的代码:

1stFragment

ActionItems

2ndFragment

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        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;
            }

    }

对于两个碎片,在MY onCreate中,我写了这一行:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        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;
            }

    }

这是我的 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"/> 中使用了 ivalidateOptionMenu() ,但徒劳无功。

0 个答案:

没有答案