Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-searchmanager

ViewPager with Fragments - SearchView doesn't respond


Solution:

The problem was due to the usage of app:showAsAction="collapseActionView|always". Replacing this with app:showAsAction="always" solved the problem.


I have a Fragment with ViewPager and the ViewPager has 3 Fragments. Each Fragment has a SearchView in its Menu Items (Displayed in the Toolbar as Menu Item). When i click on the SearchView, i get the click event in onOptionsItemSelected, but the SearchView doesn't expand. Not sure what to do.

I have set setHasOptionMenu(true) in onCreate() for all the Fragments.

Here is my current code: for each Fragment

onCreateOptionsMenu:

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

        SearchManager searchManager = (SearchManager) activity.
                getSystemService(Context.SEARCH_SERVICE);
        searchMenuItem = menu.findItem(R.id.ic_action_search);
        searchView = (SearchView) searchMenuItem.getActionView();

        searchView.setSearchableInfo(searchManager.
                getSearchableInfo(activity.getComponentName()));
        searchView.setOnQueryTextListener(this);
    }

Menu XML file:

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

    <item
        android:id="@+id/ic_action_search"
        android:icon="@drawable/ic_search_white_24dp"
        android:orderInCategory="0"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|always" />

    <item
        android:id="@+id/action_list"
        android:icon="@drawable/ic_action_list_white"
        android:orderInCategory="1"
        android:title="@string/action_list"
        android:checked="true"
        android:checkable="true"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_grid"
        android:icon="@drawable/ic_action_grid_white"
        android:orderInCategory="2"
        android:title="@string/action_grid"
        android:checked="false"
        android:checkable="true"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_mini"
        android:icon="@drawable/ic_action_grid_white"
        android:orderInCategory="3"
        android:title="@string/action_mini"
        android:checked="false"
        android:checkable="true"
        app:showAsAction="never" />

</menu>

Other related code: if it helps

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

    @Override
    public boolean onQueryTextChange(String newText) {
        mViewPagerPetitionsRecyclerViewAdapter.getFilter().filter(newText);
        return true;
    }


@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.action_list) {
            prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW).apply();
            mStaggeredLayoutManager.setSpanCount(1);
        } else if (item.getItemId() == R.id.action_grid) {
            prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.GRID_VIEW).apply();
            mStaggeredLayoutManager.setSpanCount(2);
        } else if (item.getItemId() == R.id.action_mini) {
            prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.MINI_VIEW).apply();
            mStaggeredLayoutManager.setSpanCount(1);
        }

        ActivityCompat.invalidateOptionsMenu(activity);
        mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
        mIMainNewPetitionsListener.onLayoutChangedListener();

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onPrepareOptionsMenu(Menu menu) {

        MenuItem list_item = menu.findItem(R.id.action_list);
        MenuItem grid_item = menu.findItem(R.id.action_grid);
        MenuItem mini_item = menu.findItem(R.id.action_mini);

        String item_type = prefs.getString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW);

        if (item_type.equalsIgnoreCase(Const.VIEWPAGER.LIST_VIEW)) {
            list_item.setChecked(true);
            grid_item.setChecked(false);
            mini_item.setChecked(false);
        } else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.GRID_VIEW)) {
            list_item.setChecked(false);
            grid_item.setChecked(true);
            mini_item.setChecked(false);
        } else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.MINI_VIEW)) {
            list_item.setChecked(false);
            grid_item.setChecked(false);
            mini_item.setChecked(true);
        }

        super.onPrepareOptionsMenu(menu);

    }

Solution

  • I am using Appcompat v7 23.1.1. I have a fragment with ViewPager(FragmentStatePagerAdapter) loading 3 fragments. I have a SearchView with in each of these 3 fragments. When i click on the SearchView, the view didn't expand.

    The problem was due to the usage of app:showAsAction="collapseActionView|always". Replacing this with app:showAsAction="always" solved the problem.

    Not sure if this is a bug, but this post and this post helped me in solving this.