6

我正在使用 SearchView,它工作正常,但只是setOnCloseListener不工作;这是我的代码

import com.actionbarsherlock.widget.SearchView.OnCloseListener;

searchView.setOnCloseListener(new OnCloseListener() {
            @Override
            public boolean onClose() {

                Toast.makeText(context, "close", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

**编辑* * **

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Used to put dark icons on light action bar
        //Create the search view
        final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Search");
        searchView.setIconifiedByDefault(true);

    //search button
    menu.add(Menu.NONE,Menu.NONE,1,"Search a word")
        .setIcon(R.drawable.abs__ic_search_api_holo_light)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        MenuItem sView =  menu.findItem(1);

        sView.setOnActionExpandListener(this);

return true;

}

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        adopter.getFilter().filter(null);
        Toast.makeText(getApplicationContext(), "collapse", Toast.LENGTH_LONG).show();
        return true; // Return true to collapse action view
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        Toast.makeText(getApplicationContext(), "Expand", Toast.LENGTH_LONG).show();
        return true; // Return true to expand action view
    }
4

4 回答 4

17

自己解决了。离开 setOnCloseListener它是行不通的,然后把下面的代码放进去onCreateOptionsMenu

//        searchView.setOnCloseListener(new OnCloseListener() { 
//          @Override
//          public boolean onClose() {
//              adapter.getFilter().filter("");
//              Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show(); 
//              return false;
//          }
//      });

        MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
        menuItem.setOnActionExpandListener(new OnActionExpandListener() {   
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                adapter.getFilter().filter("");
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                adapter.getFilter().filter("");
                return true;
            }
        });
于 2013-09-24T06:30:57.547 回答
2

我遇到了与 onCloseListener 不调用 SearchView 相同的问题。从25758中提出的错误问题和我阅读的一些帖子中了解,要调用 onCloseListener,您需要设置:

searchView.setIconifiedByDefault(true);

但就我而言,我想让搜索视图一直打开而不是图标化。我设法通过在下面再添加一行来解决这个问题:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_bar, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setOnQueryTextListener(queryTextListener);
    searchView.setIconifiedByDefault(true);
    searchView.setIconified(false);
    return true;
}

searchView.setIconified(false) 将导致 searchView 打开,尽管在上一行中将默认设置为 iconified 为 true。通过这种方式,我设法同时拥有一个始终打开的 SearchView 并让它调用 onCloseListener。

于 2014-08-14T01:23:21.917 回答
1

我正在使用这段代码。它完美地工作

    @Override
    public void onStartSearch() {
    }

    @Override
    public void onSearch(String search) {

    }

    @Override
    public void onCloseSearch() {
    }

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

        // Associate searchable configuration with the SearchView
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); //here I user appcompat, but you can take it just from actionbarsherlock

        SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        searchAutoComplete.setTextColor(getResources().getColor(R.color.action_bar_text_color));

        mSearchView.setIconifiedByDefault(true);
        mSearchView.setSubmitButtonEnabled(false);

        /**
         * Set all of different kinds of listeners
         */
        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                menuIsOpen = false;
                onCloseSearch();
                return true;
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                menuIsOpen = true;
                onStartSearch();
                return true;
            }
        });

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String s) {
                onSearch(s);
                return false;
            }

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

        super.onCreateOptionsMenu(menu, inflater);
    }

/**
     * Function for closing search when android is less than 14
     */
    public boolean onBackButton() {
        if (AndroidUtils.getSdkVersion() >= 14)
            return false;

        if (menuIsOpen) {
            menuIsOpen = false;
            getActivity().supportInvalidateOptionsMenu();
            onCloseSearch();
            return true;
        }
        return false;
    }

在 backButton 之后它关闭搜索,或者只是从操作栏中选择关闭。

于 2013-09-11T17:13:31.003 回答
0

您可以使用 OnActionExpandListener:

public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
  SearchView searchView = new SearchView(((SherlockFragmentActivity) getActivity()).getSupportActionBar().getThemedContext());
  searchView.setIconifiedByDefault(true);
  // ...
  MenuItem menuItem = menu.add(R.string.search);
  // ...
  menuItem.setOnActionExpandListener(this);
}

@Override
public boolean onMenuItemActionExpand(final MenuItem item) {
    mInSearchMode = true;
    return true;
}

@Override
public boolean onMenuItemActionCollapse(final MenuItem item) {
    mInSearchMode = false;
    return true;
}
于 2013-08-09T10:37:31.607 回答