示例#1
0
  public void restoreActionViewStates(Bundle states) {
    if (states == null) {
      return;
    }

    SparseArray<Parcelable> viewStates = states.getSparseParcelableArray(getActionViewStatesKey());

    final int itemCount = size();
    for (int i = 0; i < itemCount; i++) {
      final MenuItem item = getItem(i);
      final View v = item.getActionView();
      if (v != null && v.getId() != View.NO_ID) {
        v.restoreHierarchyState(viewStates);
      }
      if (item.hasSubMenu()) {
        final SubMenuBuilder subMenu = (SubMenuBuilder) item.getSubMenu();
        // subMenu.restoreActionViewStates(states);
      }
    }

    final int expandedId = states.getInt(EXPANDED_ACTION_VIEW_ID);
    if (expandedId > 0) {
      MenuItem itemToExpand = findItem(expandedId);
      if (itemToExpand != null) {
        itemToExpand.expandActionView();
      }
    }
  }
  /** loads the weather-data from the internet and stores it in the shared preferences. */
  private void reloadWeatherInfo(final MenuItem item) {
    // start refresh-icon-spinner
    item.setActionView(R.layout.action_progressbar);
    item.expandActionView();

    int numberOfActiveRegions = 0;
    for (final Region region : regions) {
      if (region.isActive()) {
        numberOfActiveRegions++;
      }
    }

    final AsyncTaskCounter counter =
        new AsyncTaskCounter(
            numberOfActiveRegions,
            new AsyncTaskCounterCallback() {
              @Override
              public void onFinishedAllTasks() {
                // stop refresh-icon-spinner
                item.collapseActionView();
                item.setActionView(null);
              }
            });

    // refresh weather data for all active regions
    for (final Region region : regions) {
      if (region.isActive()) {
        final DownloadWeatherDataAsyncTask task =
            new DownloadWeatherDataAsyncTask(this, sharedPrefs, region, counter);
        task.execute(new URL[] {});
      }
    }
  }
示例#3
0
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_words, menu);

    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    MenuItemCompat.setOnActionExpandListener(
        searchMenuItem,
        new MenuItemCompat.OnActionExpandListener() {
          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
          }

          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            updateData();
            return true;
          }
        });

    mSearchView = (SearchView) searchMenuItem.getActionView();
    mSearchView.setQueryHint(getString(R.string.search_hint));

    if (mFilterText != null) {
      String query = mFilterText;
      searchMenuItem.expandActionView();
      mSearchView.setQuery(query, true);
      mSearchView.clearFocus();
    }
    setSearchViewTextChangeListener();
  }
示例#4
0
 @Override
 public boolean onKeyUp(int key, KeyEvent event) {
   if (key == KeyEvent.KEYCODE_SEARCH) {
     MenuItem item = menu.findItem(R.id.search);
     item.expandActionView();
   }
   return super.onKeyUp(key, event);
 }
示例#5
0
 @Override
 protected void onPreExecute() {
   super.onPreExecute();
   if (Params.TAG_FG_DEBUG && fgDebugLocal) {
     Log.i(Params.TAG_GEN, TAG_LOCAL + "onPreExecute");
   }
   ;
   menuItem.setActionView(R.layout.lanceur_progressbar);
   menuItem.expandActionView();
 }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mToolbar.inflateMenu(R.menu.menu_search);
    mToolbar.inflateMenu(R.menu.menu_others);

    RelativeLayout progressBar = (RelativeLayout) getActivity().findViewById(R.id.progressBarFetch);
    progressBar.setVisibility(View.GONE);

    searchButton = mToolbar.getMenu().findItem(R.id.action_search);
    searchButton.expandActionView();

    bigFloatingActionButton.hide();

    searchView = (SearchView) searchButton.getActionView();
    MenuItemCompat.setOnActionExpandListener(
        searchButton,
        new MenuItemCompat.OnActionExpandListener() {
          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            bigFloatingActionButton.show();
            return true;
          }

          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            bigFloatingActionButton.hide();
            return true;
          }
        });
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String query) {
            if (query.length() <= 2) {
              Toast.makeText(getActivity(), R.string.please_inset_more, Toast.LENGTH_SHORT).show();
            } else {
              initData(query);
              searchView.clearFocus();
              bigFloatingActionButton.show();
            }
            // searchButton.collapseActionView(); //ToDO
            return true;
          }

          @Override
          public boolean onQueryTextChange(String newText) {
            // mAdapter.filter(newText); //ToDO
            return true;
          }
        });

    setList();
  }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_load:
       menuItem = item;
       menuItem.setActionView(R.layout.progressbar);
       menuItem.expandActionView();
       TestTask task = new TestTask();
       task.execute("test");
       break;
     default:
       break;
   }
   return true;
 }
示例#8
0
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.menu_load:
       menuItem = item;
       menuItem.setActionView(R.layout.progressbar);
       menuItem.expandActionView();
       wv.reload();
       Toast.makeText(this, "Refreshing :)", Toast.LENGTH_LONG).show();
       break;
     case android.R.id.home:
       finish(); // remove from the stack
       //			NavUtils.navigateUpFromSameTask(this);
       break;
     default:
       return super.onOptionsItemSelected(item);
   }
   return true;
 }
示例#9
0
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);

    MenuItem menuItem = menu.findItem(R.id.action_search);
    menuItem.expandActionView();
    MenuItemCompat.setOnActionExpandListener(
        menuItem,
        new MenuItemCompat.OnActionExpandListener() {
          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            return false;
          }

          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            SearchActivity.this.finish();
            return false;
          }
        });
    searchView = (SearchView) menuItem.getActionView();
    searchView.setQueryHint(this.getResources().getString(R.string.action_search));
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {
          private TextView textView = (TextView) findViewById(R.id.text_query_search);

          @Override
          public boolean onQueryTextSubmit(String query) {
            inputMethodManager.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            return true;
          }

          @Override
          public boolean onQueryTextChange(String newText) {
            textView.setText(newText);
            return true;
          }
        });
    this.getTypeSearch();
    return true;
  }
示例#10
0
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();

    // collapse search
    Context context = getSupportActionBar().getThemedContext();
    View searchView = SearchViewCompat.newSearchView(context);

    if (searchView != null) {

      SearchViewCompat.setOnQueryTextListener(
          searchView,
          new OnQueryTextListenerCompat() {
            @Override
            public boolean onQueryTextChange(String newText) {
              nearFragment.setFilter(newText);
              return true;
            }

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

    MenuItem searchItem = menu.add("search").setIcon(R.drawable.ic_action_search);
    searchItem.setShowAsAction(
        MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    searchItem.setActionView(searchView);
    searchItem.expandActionView();

    inflater.inflate(R.menu.menu, menu);

    return true;
  }
示例#11
0
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchItem.expandActionView();
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String query) {
            //  swipeRefreshLayout.setRefreshing(true);
            progressBar.setVisibility(View.VISIBLE);
            queryFeeds(query);
            return false;
          }

          @Override
          public boolean onQueryTextChange(String newText) {
            return false;
          }
        });

    return true;
  }
示例#12
0
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case R.id.offline:
       if (prefs.getBoolean("hideOffline", false)) service.setPreference("hideOffline", false);
       else service.setPreference("hideOffline", true);
       updateMenu();
       updateList();
       break;
     case R.id.status:
       RosterDialogs.changeStatusDialog(this, null, null);
       break;
     case android.R.id.home:
       RosterDialogs.changeStatusDialog(this, null, null);
       break;
     case R.id.add:
       RosterDialogs.addDialog(this, null);
       break;
     case R.id.search:
       menu.removeItem(R.id.chats);
       item.expandActionView();
       break;
     case R.id.bookmarks:
       Intent bIntent = new Intent(this, Bookmarks.class);
       startActivity(bIntent);
       break;
     case R.id.chats:
       ChangeChatDialog.show(this);
       break;
     case R.id.accounts:
       Intent aIntent = new Intent(this, Accounts.class);
       startActivity(aIntent);
       break;
     case R.id.prefs:
       startActivityForResult(new Intent(this, Preferences.class), ACTIVITY_PREFERENCES);
       break;
     case R.id.disco:
       startActivity(new Intent(this, ServiceDiscovery.class));
       break;
     case R.id.notes:
       startActivity(new Intent(this, NotesActivity.class));
       break;
     case R.id.xml:
       startActivity(new Intent(this, XMLConsole.class));
       break;
     case R.id.notify:
       if (prefs.getBoolean("soundDisabled", false)) service.setPreference("soundDisabled", false);
       else service.setPreference("soundDisabled", true);
       updateMenu();
       break;
     case R.id.exit:
       if (prefs.getBoolean("DeleteHistory", false)) {
         getContentResolver().delete(JTalkProvider.CONTENT_URI, null, null);
       }
       Notify.cancelAll(this);
       stopService(new Intent(this, JTalkService.class));
       finish();
       System.exit(0);
       break;
     default:
       return false;
   }
   return true;
 }
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  public void setupSearch(Menu menu) {
    // bail early on unsupported devices
    if (Build.VERSION.SDK_INT < 11) {
      return;
    }

    mSearchItem = menu.findItem(R.id.search);
    MenuItemCompat.setOnActionExpandListener(
        mSearchItem,
        new OnActionExpandListener() {

          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            // always allow expansion of the search action view
            return mCurrentPageId == R.id.task_group_search;
          }

          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            // return to previous view
            if (mPreviousPagePosition >= 0 && mCurrentPageId == R.id.task_group_search) {
              mViewPager.setCurrentItem(mPreviousPagePosition);
              mCurrentPageId = mPagerAdapter.getPageId(mPreviousPagePosition);
            }
            return mPreviousPagePosition >= 0 || mCurrentPageId != R.id.task_group_search;
          }
        });
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if (null != searchManager) {
      searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }

    searchView.setQueryHint(getString(R.string.menu_search_hint));
    searchView.setIconified(true);
    searchView.setOnQueryTextListener(
        new OnQueryTextListener() {

          @Override
          public boolean onQueryTextSubmit(String query) {
            // persist current search
            mSearchHistoryHelper.commitSearch();
            mHandler.post(mSearchUpdater);
            return true;
          }

          @Override
          public boolean onQueryTextChange(String query) {
            if (mCurrentPageId != R.id.task_group_search) {
              return true;
            }

            mHandler.removeCallbacks(mSearchUpdater);
            if (query.length() > 0) {
              mSearchHistoryHelper.updateSearch(query);
              mHandler.postDelayed(mSearchUpdater, SEARCH_UPDATE_DELAY);
            } else {
              mSearchHistoryHelper.removeCurrentSearch();
              mHandler.post(mSearchUpdater);
            }
            return true;
          }
        });

    if (mAutoExpandSearchView) {
      mSearchItem.expandActionView();
    }
  }
示例#14
0
 @Override
 public boolean expandActionView() {
   return mNativeItem.expandActionView();
 }
  // 设置搜索条
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_searchview);

    // 打开,直接显示搜索条
    searchItem.expandActionView();
    searchView = (SearchView) searchItem.getActionView();
    // 设置搜索条提示文字
    searchView.setQueryHint("Please enter keyword");
    searchView.setOnQueryTextListener(
        new OnQueryTextListener() {

          @Override
          public boolean onQueryTextSubmit(final String newText) {
            final ListView lv1 = (ListView) mViewPager.findViewWithTag("lv1");
            final ListView lv2 = (ListView) mViewPager.findViewWithTag("lv2");
            final ListView lv3 = (ListView) mViewPager.findViewWithTag("lv3");

            // progress dialog + thread
            dialog = ProgressDialog.show(SearchMainActivity.this, null, "Searching...", true);

            new Thread(
                    new Runnable() {

                      @Override
                      public void run() {
                        // TODO Auto-generated method stub
                        processChange(newText);

                        mHandler.post(
                            new Runnable() {

                              @Override
                              public void run() {
                                // TODO Auto-generated method stub
                                if (lv1 != null) {
                                  topicAdapter.notifyDataSetChanged();
                                  UpdateViewPager();
                                }
                                if (lv2 != null) {
                                  questionAdapter.notifyDataSetChanged();
                                  UpdateViewPager();
                                }
                                if (lv3 != null) {
                                  sampleAdapter.notifyDataSetChanged();
                                  UpdateViewPager();
                                }
                                hideKeyBoard();
                              }
                            });
                        dialog.dismiss();
                      }
                    })
                .start();

            return true;
          }

          @Override
          public boolean onQueryTextChange(String newText) {
            return false;
          }
        });

    searchItem.setOnActionExpandListener(
        new OnActionExpandListener() {

          @Override
          public boolean onMenuItemActionExpand(MenuItem item) {
            // Toast.makeText(SearchMainActivity.this, "expand",
            // Toast.LENGTH_LONG).show();
            return true;
          }

          @Override
          public boolean onMenuItemActionCollapse(MenuItem item) {
            // Toast.makeText(SearchMainActivity.this,"collapse",Toast.LENGTH_SHORT).show();
            return true;
          }
        });

    return super.onCreateOptionsMenu(menu);
  }
 public void expandSearch() {
   if (mSearch != null) {
     mSearch.expandActionView();
   }
 }
示例#17
0
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    final FragmentManager fm = getFragmentManager();

    final RootInfo root = getCurrentRoot();
    final DocumentInfo cwd = getCurrentDirectory();

    final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
    final MenuItem search = menu.findItem(R.id.menu_search);
    final MenuItem sort = menu.findItem(R.id.menu_sort);
    final MenuItem sortSize = menu.findItem(R.id.menu_sort_size);
    final MenuItem grid = menu.findItem(R.id.menu_grid);
    final MenuItem list = menu.findItem(R.id.menu_list);
    final MenuItem advanced = menu.findItem(R.id.menu_advanced);
    final MenuItem fileSize = menu.findItem(R.id.menu_file_size);

    sort.setVisible(cwd != null);
    grid.setVisible(mState.derivedMode != MODE_GRID);
    list.setVisible(mState.derivedMode != MODE_LIST);

    if (mState.currentSearch != null) {
      // Search uses backend ranking; no sorting
      sort.setVisible(false);

      search.expandActionView();

      mSearchView.setIconified(false);
      mSearchView.clearFocus();
      mSearchView.setQuery(mState.currentSearch, false);
    } else {
      mIgnoreNextClose = true;
      mSearchView.setIconified(true);
      mSearchView.clearFocus();

      mIgnoreNextCollapse = true;
      search.collapseActionView();
    }

    // Only sort by size when visible
    sortSize.setVisible(mState.showSize);

    final boolean searchVisible;
    if (mState.action == ACTION_CREATE || mState.action == ACTION_OPEN_TREE) {
      createDir.setVisible(cwd != null && cwd.isCreateSupported());
      searchVisible = false;

      // No display options in recent directories
      if (cwd == null) {
        grid.setVisible(false);
        list.setVisible(false);
      }

      if (mState.action == ACTION_CREATE) {
        /// M: add to avoid seldom NullPointerException
        SaveFragment saveFragment = SaveFragment.get(fm);
        if (null != saveFragment) {
          saveFragment.setSaveEnabled(cwd != null && cwd.isCreateSupported());
        } else {
          Log.e(TAG, "onPrepareOptionsMenu, SaveFragment is null");
        }
      }
    } else {
      createDir.setVisible(false);

      searchVisible = root != null && ((root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0);
    }

    // TODO: close any search in-progress when hiding
    search.setVisible(searchVisible);

    advanced.setTitle(
        LocalPreferences.getDisplayAdvancedDevices(this)
            ? R.string.menu_advanced_hide
            : R.string.menu_advanced_show);
    fileSize.setTitle(
        LocalPreferences.getDisplayFileSize(this)
            ? R.string.menu_file_size_hide
            : R.string.menu_file_size_show);

    /// M: If in force show advance mode, need hide advance menu
    advanced.setVisible(mState.action != ACTION_MANAGE && !mState.forceAdvanced);
    /// M: If cmd == null and mState.action == ACTION_CREATE, it means show
    /// RecentsCreateFragment, we need hide file size menu to avoid exception
    fileSize.setVisible(
        mState.action != ACTION_MANAGE && !(cwd == null && mState.action == ACTION_CREATE));

    return true;
  }
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);

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

    // Retrieves the system search manager service
    final SearchManager searchManager =
        (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);

    // Retrieves the SearchView from the search menu item
    final SearchView searchView = (SearchView) searchItem.getActionView();

    // Assign searchable info to SearchView
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

    // Set listeners for SearchView
    searchView.setOnQueryTextListener(
        new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String queryText) {
            // Nothing needs to happen when the user submits the search string
            return true;
          }

          @Override
          public boolean onQueryTextChange(String newText) {
            // Called when the action bar search text has changed.  Updates the search filter, and
            // restarts the loader to do a new query
            // using the new search string.
            String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

            // Don't do anything if the filter is empty
            if (mSearchTerm == null && newFilter == null) {
              return true;
            }

            // Don't do anything if the new filter is the same as the current filter
            if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
              return true;
            }

            // Updates current filter to new filter
            mSearchTerm = newFilter;

            // Restarts the loader. This triggers onCreateLoader(), which builds the necessary
            // content Uri from mSearchTerm.
            mSearchQueryChanged = true;
            getLoaderManager()
                .restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
            return true;
          }
        });

    // This listener added in ICS
    searchItem.setOnActionExpandListener(
        new MenuItem.OnActionExpandListener() {
          @Override
          public boolean onMenuItemActionExpand(MenuItem menuItem) {
            // Nothing to do when the action item is expanded
            return true;
          }

          @Override
          public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            // When the user collapses the SearchView the current search string is
            // cleared and the loader restarted.
            if (!TextUtils.isEmpty(mSearchTerm)) {
              onSelectionCleared();
            }
            mSearchTerm = null;
            getLoaderManager()
                .restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
            return true;
          }
        });

    if (mSearchTerm != null) {
      // If search term is already set here then this fragment is being restored from a saved state
      // and the search menu item
      // needs to be expanded and populated again. Stores the search term (as it will be wiped out
      // by onQueryTextChange() when
      // the menu item is expanded).
      final String savedSearchTerm = mSearchTerm;

      searchItem.expandActionView();

      // Sets the SearchView to the previous search string
      searchView.setQuery(savedSearchTerm, false);
    }
  }
  // This method uses APIs from newer OS versions than the minimum that this
  // app supports. This
  // annotation tells Android lint that they are properly guarded so they
  // won't run on older OS
  // versions and can be ignored by lint.
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contacts_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent
    // additional
    // searches. In Android 3.0 and later, searching is done via a
    // SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching,
    // the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
      searchItem.setVisible(false);
    }

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

      // Retrieves the SearchView from the search menu item
      final SearchView searchView = (SearchView) searchItem.getActionView();

      // Assign searchable info to SearchView
      searchView.setSearchableInfo(
          searchManager.getSearchableInfo(getActivity().getComponentName()));

      // Set listeners for SearchView
      searchView.setOnQueryTextListener(
          new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
              // Nothing needs to happen when the user submits the
              // search string
              return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
              // Called when the action bar search text has
              // changed. Updates
              // the search filter, and restarts the loader to do
              // a new query
              // using the new search string.
              String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

              // Don't do anything if the filter is empty
              if (mSearchTerm == null && newFilter == null) {
                return true;
              }

              // Don't do anything if the new filter is the same
              // as the current filter
              if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                return true;
              }

              // Updates current filter to new filter
              mSearchTerm = newFilter;

              // Restarts the loader. This triggers
              // onCreateLoader(), which builds the
              // necessary content Uri from mSearchTerm.
              mSearchQueryChanged = true;
              getLoaderManager()
                  .restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
              return true;
            }
          });

      if (Utils.hasICS()) {
        // This listener added in ICS
        searchItem.setOnActionExpandListener(
            new MenuItem.OnActionExpandListener() {
              @Override
              public boolean onMenuItemActionExpand(MenuItem menuItem) {
                // Nothing to do when the action item is
                // expanded
                return true;
              }

              @Override
              public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                // When the user collapses the SearchView the
                // current search string is
                // cleared and the loader restarted.
                if (!TextUtils.isEmpty(mSearchTerm)) {
                  onSelectionCleared();
                }
                mSearchTerm = null;
                getLoaderManager()
                    .restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
              }
            });
      }

      if (mSearchTerm != null) {
        // If search term is already set here then this fragment is
        // being restored from a saved state and the search menu item
        // needs to be expanded and populated again.

        // Stores the search term (as it will be wiped out by
        // onQueryTextChange() when the menu item is expanded).
        final String savedSearchTerm = mSearchTerm;

        // Expands the search menu item
        if (Utils.hasICS()) {
          searchItem.expandActionView();
        }

        // Sets the SearchView to the previous search string
        searchView.setQuery(savedSearchTerm, false);
      }
    }
  }