如何在操作栏

时间:2017-05-09 21:42:26

标签: android menuitem searchview

我的应用有3个标签,tab3菜单项有所不同。 所以我所做的是我在活动中夸大了菜单项,并根据片段菜单项设置了visibilty。

活动代码:

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

Menu_home_page.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/menu_plan_your_day"
    android:icon="@drawable/ic_settings_white_18dp"
    android:title="@string/action_notify"
    android:visible="false"
    app:showAsAction="always" />
<item
    android:id="@+id/menu_notification"
    android:icon="@drawable/ic_notifications_white_18dp"
    android:title="@string/action_notify"
    android:visible="false"
    app:showAsAction="always" />
<item
    android:id="@+id/menu_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="@string/menu_search"
    android:visible="false"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always" />


</menu>

Tab1(片段代码):

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
       getActivity().invalidateOptionsMenu();
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        final MenuItem contactsSearchMenu = menu.findItem(R.id.menu_search);
        final MenuItem planYourDayMenu = menu.findItem(R.id.menu_plan_your_day);
        final MenuItem notificationMenu = menu.findItem(R.id.menu_notification);
        contactsSearchMenu.setVisible(false);
        planYourDayMenu.setVisible(true);
        notificationMenu.setVisible(true);

    }

Tab2代码与Tab1相同

Tab3代码:

public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        if (menu != null) {
            final MenuItem contactsSearchMenu = menu.findItem(R.id.menu_search);
            final MenuItem planYourDayMenu = menu.findItem(R.id.menu_plan_your_day);
            final MenuItem notificationMenu = menu.findItem(R.id.menu_notification);
            contactsSearchMenu.setVisible(true);
            planYourDayMenu.setVisible(false);
            notificationMenu.setVisible(false);
        }

    }


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);


    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager =
                (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
        final SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getActivity().getComponentName()));


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {

                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }


                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                mSearchTerm = newFilter;

                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(
                        ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Utils.hasICS()) {
            MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {

                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {

                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(
                            ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });

        }

        if (mSearchTerm != null) {
            final String savedSearchTerm = mSearchTerm;

            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/HomePageAppbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"></android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/HomePageTab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            app:tabMode="fixed" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/HomePageViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>


</LinearLayout>

但问题是只有searchview菜单项对所有三个标签都可见。 如何隐藏tab1和tab2的searchview菜单项?

2 个答案:

答案 0 :(得分:1)

这并不难。 searchViewView,因此您可以在View中使用班级searchView及其属性和功能。

Here is the documentation。查找setVisibility function

如果您要查找菜单项而不是searchView本身,则必须将setVisible(false)放入public void onPrepareOptionsMenu(Menu menu)功能。

答案 1 :(得分:1)

每次ViewPager发生更改时,您都可以监控页面更改并调用invalidateOptions菜单

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    invalidateOptionsMenu();
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }


        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {

            if (viewPager.getCurrentItem() == 1 || viewPager.getCurrentItem() == 2) {
                contactsSearchMenu.setVisible(false);
            } else {
                contactsSearchMenu.setVisible(false);
            }

            return super.onPrepareOptionsMenu(menu);
        }