¿Cómo interceptar un clic en el botón "Arriba" (botón de inicio) en ActionBarSherlock?

I have implemented a search feature for one of my project using Barra de acción Sherlock. So now I have the search edit text on the ActionBar like in the samples of the official AB sherlock project repo (on github) : Collapsible action items

My search edit text is filtering a ListView within the activity. When the user starts to type in some characters, the ListView gets filtered. I did it by adding a textWatcher to the search Edit text like this :

  public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(Menu.NONE, SEARCH_ACTION_MENU, 0, "Search")
                | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    /** We get a reference to the search edit */
    MenuItemWrapper menuItemWrapper = (MenuItemWrapper) menu.getItem(0);
    final EditText searchEdit = (EditText) menuItemWrapper.getActionView();
    /** We add a listener search filter listener */
    searchEdit.addTextChangedListener(filterWatcher); // filterwatcher is a textWatcher
    return true;

When I click on the search icon, the search Edit Text is shown and the user can start to type in things. Also, the home button is displayed as an "Up Home button" (with the left arrow).

Clicking on this arrow will make the search text view disappear but if the user has already started to type in things, the List View is filtered and is not reset when the search Text View is not visible anymore after the user clicks on the home "Up" button.

Mi pregunta es : how can I intercept click on the "Arriba" button to reset my List View ? I think this is the only way I can reset my list.

Ya lo he intentado:

  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        Log.d(Constants.LOG_TAG, "Home button clicked");
        return true;

        return super.onOptionsItemSelected(item);

It is not working when the "Home button" is displayed as "Up" button.

Cualquier ayuda sería apreciada.

gracias por adelantado,

preguntado el 31 de julio de 12 a las 13:07

Sorry if I misunderstand, but is it the physical android home button you wan to intercept? -

@AndersMetnik I want to intercept the click on the "Up" button on the ActionBar (ActionBar Sherlock) not the physical one. Please have a look at the bottom image of this link (the left-most button) : developer.android.com/images/ui/actionbar-searchview.png -

2 Respuestas

Necesitas implementar CollapsibleActionView on your view in order to receive the onActionViewCollapsed() llamar de vuelta.

You should also upgrade to the version of ActionBarSherlock that is on the dev branch since it has crucial fixes for collapsible action items when used on the native action bar.

Respondido 01 ago 12, 10:08

Thanks a lot, it solved my issue. Actually, I implemented it like this : menuItemWrapper.setOnActionExpandListener(new OnActionExpandListener() { // my logic here }); entonces en el public boolean onMenuItemActionCollapse(MenuItem item) method of the listener, I just added the code to reset my search filter. Again thanks :-) - cafeína

Debería funcionar con onOptionsItemSelected.

Asegúrate de estar usando com.actionbarsherlock.view.MenuItem.

También podrías usar onMenuItemSelected(int featureId, MenuItem item) del mismo modo.

Respondido 31 Jul 12, 13:07

I am using com.actionbarsherlock.view.MenuItem but still the click on the "Up" button is now intercepted but if the button is in a "normal" mode (displayed as a simple icon without the left arrow) it's intercepted - cafeína

is now or is no intercepted? - benito bertoli

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.