Action Bar Sherlock SearchView not expanding on click of it

末鹿安然 提交于 2019-12-01 03:37:33

We generally use onCreateOptionsMenu() for creating Options Menu for fragments and activities. If you go through the documentation, there's another helper method called onPrepareOptionsMenu() which, as per the documentation says:

Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.

SearchView expanding/collapsing works fine when we are not instantiating fragments inside ViewPager or anything similar tab like switching scenarios. I've had the same problem once. I came up with using onPrepareOptionsMenu() to resolve my problem.

Just implement onPrepareOptionsMenu() in each of your fragments and inside of it, call onQueryTextChange("") passing "" as query string. It will do the trick to suggest the fragment container activity that this fragment wants to mind his own searching business.

OR , Not sure, but a call to invalidateOptionsMenu() on container activity could also come handy.

Let me know if that helps.

EDIT:

My Implementation:

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

        SearchView searchView = new SearchView(getActivity());
        searchView.setQueryHint("Search for Events");
        searchView.setOnQueryTextListener(this);

        menu.add(0, ACTION_SEARCH, 0, "Search")
                .setIcon(R.drawable.ic_mailbox_search)
                .setActionView(searchView)
                .setOnActionExpandListener(menuExpandListener)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

@Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        onQueryTextChange("");
    }

@Override
    public boolean onQueryTextSubmit(String query) {
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {

        if (mAdapter != null)
            mAdapter.getFilter().filter(newText);
        return false;
    }

I have achieved what i want finally but with one problem.. I don't know whatever i have done is the hack/patch or the correct way but it is working fine in <3.0 but now find the issue in >=3.0.

What i have done is ::

In My Container Activity i have declare the onCreateOptionsMenu() and onPrepareOptionsMenu() ::

@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        if(versionStatus==0)
        {
            menu.findItem(R.id.root_menu).setVisible(false);
        }
        return super.onPrepareOptionsMenu(menu);
    }

Now in each Fragment's onCreateOptionsMenu() i have done something like this ::
I have checked the build version of OS and based on that i manupulate my option menu like below ..

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
    {
        super.onCreateOptionsMenu(menu, inflater);
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        int versionStatus = 0;
        if (currentapiVersion == android.os.Build.VERSION_CODES.FROYO || currentapiVersion == android.os.Build.VERSION_CODES.GINGERBREAD)
        {
            versionStatus=0;
        }
        else if(currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB)
        {
            versionStatus=1;
        }
        if(versionStatus==1)
        {
            inflater.inflate(R.menu.menu, menu);
            menu.findItem(R.id.menu_item_search).setVisible(false);

            final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
            SearchView searchView = (SearchView) searchItem.getActionView();
            searchView.setQueryHint("Search Products");
            searchView.setIconifiedByDefault(false);
            searchView.setOnQueryTextListener(this);
        }
        else
        {
            inflater.inflate(R.menu.menu, menu);
            final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
            SearchView searchView = (SearchView) searchItem.getActionView();
            searchView.setQueryHint("Search Products");
            searchView.setIconifiedByDefault(false);
            searchView.setOnQueryTextListener(this);
        }
    }

It's working fine in 2.3.3 but problem m facing now is in 3.0 +

1)not able to see the hint after expanding the searchview
2)expand the searchview in first fragment...

3)Move to second fragment(without collapsing the searchview )

4)Now in second fragment expand the searchview of current fragment

5)in Current Fragment Pressed Back then the result is mess :(

EDIT ::

I have done the following changes in my Container Activity and now its working fine,Finally i have achieved what i want....

  @Override
        public boolean onCreateOptionsMenu(Menu menu) 
        {
            super.onCreateOptionsMenu(menu);
            if(versionStatus==0)
            {
                getSupportMenuInflater().inflate(R.menu.menu, menu);
            }
            return true;
        }
        public boolean onPrepareOptionsMenu(Menu menu) 
        {
            if(versionStatus==0)
            {
                menu.findItem(R.id.root_menu).setVisible(false);
            }
            return super.onPrepareOptionsMenu(menu);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!