简体   繁体   中英

Action Bar Sherlock SearchView not expanding on click of it

I have one Sherlock Fragment Activity from which i am setting the different sherlock fragments using fragment pager adapter.

Now to show the searchview in each of the fragment i have placed this method in onCreate of Fragment::

This method will show menu items in the ActionBar of the fragment.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

I have now also set the onCreateOptionMenu() like this ::

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

    inflater.inflate(R.menu.menu, menu);
    final MenuItem searchItem = menu.findItem(R.id.menu_item_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Search Here");
}

Now the strange things happens that in my first fragment i have done the same thing and the searchview get opened successfully.

If

  • i go to the first fragment,open the searchview then go to the second
  • fragment the searchview will get opened.

Same thing happens for third fragment

  • go to 3rd fragment,
  • open searchview,
  • back to the second frgment,searchview get opened.

But if i go to directly on the second fragment the searchview not get opened.

Hope i am clear.

Any clue about this? Any suggestion/links will be appreciated..

Thanks in Advance...

EDIT ::

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

   <item
        android:id="@+id/menu_item_search"
        android:icon="@drawable/abs__ic_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView" 
        android:title="Search Products">
    </item> 
    <item
        android:id="@+id/root_menu"
        android:icon="@drawable/abs__ic_menu_moreoverflow_normal_holo_light"
        android:showAsAction="always"
        android:title="More">
        <menu>
            <item
                android:id="@+id/menu_Home"
                android:icon="@drawable/home"
                android:showAsAction="never"
                android:title="Home"/>
            <item
                android:id="@+id/menu_favourite"
                android:icon="@drawable/favourite"
                android:showAsAction="never"
                android:title="Favourite"/>
            <item
                android:id="@+id/menu_Balance"
                android:icon="@drawable/balance"
                android:showAsAction="never"
                android:title="Balance"/>
            <item
                android:id="@+id/menu_logout"
                android:icon="@drawable/btn_logout"
                android:showAsAction="never"
                android:title="Logout"/>
        </menu>
    </item>

</menu>

Logcat ::

    12-06 16:45:26.401: E/AndroidRuntime(1581): FATAL EXCEPTION: main
12-06 16:45:26.401: E/AndroidRuntime(1581): java.lang.ClassCastException: com.mobifin.subscriber.ButtonPayActivity
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.mobifin.subscriber.home.BillPayFragment.onCreateOptionsMenu(BillPayFragment.java:101)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:55)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.internal.ActionBarSherlockCompat.preparePanel(ActionBarSherlockCompat.java:466)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.internal.ActionBarSherlockCompat.dispatchInvalidateOptionsMenu(ActionBarSherlockCompat.java:265)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.app.SherlockFragmentActivity.invalidateOptionsMenu(SherlockFragmentActivity.java:149)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.actionbarsherlock.app.SherlockFragmentActivity.supportInvalidateOptionsMenu(SherlockFragmentActivity.java:155)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.app.Fragment.setMenuVisibility(Fragment.java:726)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:127)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.view.ViewPager.populate(ViewPager.java:802)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1280)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:1176)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1562)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.View.draw(View.java:6883)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.widget.FrameLayout.draw(FrameLayout.java:357)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewRoot.draw(ViewRoot.java:1522)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.os.Looper.loop(Looper.java:130)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at android.app.ActivityThread.main(ActivityThread.java:3683)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at java.lang.reflect.Method.invoke(Method.java:507)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 16:45:26.401: E/AndroidRuntime(1581):     at dalvik.system.NativeStart.main(Native Method)

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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM