2

ActionbarSherlockver4.2で「検索」しようとしています。ActionbarSherlockは、最新バージョンでSerchViewをバックポートしました。

私は次のコードを持っていonCreateOptionsMenuますSherlockListFragment

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Place an action bar item for searching.
        SearchView searchView = new SearchView(getSherlockActivity().getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Search Friends");
        searchView.setIconified(true);

        menu.add(Menu.NONE, Menu.FIRST, Menu.FIRST, "Refresh")
                .setIcon(R.drawable.ic_action_refresh)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE, Menu.FIRST + 1, Menu.FIRST + 1, "Search")
                .setIcon(R.drawable.abs__ic_search)
                .setActionView(searchView)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    }

および次のコード

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case Menu.FIRST:
            Toast.makeText(getActivity(),"FIRST", Toast.LENGTH_SHORT).show();
            break;
         case Menu.FIRST + 1:
             Toast.makeText(getActivity(),"FIRST+1", Toast.LENGTH_SHORT).show();
            break;
    }
    return super.onOptionsItemSelected(item);
}

ABSの[更新]ボタンをクリックするとToastが表示されますが、[検索]をクリックすると展開されてEditTextに変換されますが、Toastが起動しません。

私の質問

アクションバーの「検索」をABSと統合する方法は?

4

2 に答える 2

11

できます。

SearchViewを実装するには、次のようなコールバックインターフェイスを実装する必要があります

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            /**
             * Called when the user submits the query. This could be due to a key press on the
             * keyboard or due to pressing a submit button.
             * The listener can override the standard behavior by returning true
             * to indicate that it has handled the submit request. Otherwise return false to
             * let the SearchView handle the submission by launching any associated intent.
             *
             * @param newText the query text that is to be submitted
             * @return true if the query has been handled by the listener, false to let the
             *         SearchView perform the default action.
             */
            @Override
            public boolean onQueryTextSubmit(String newText) {

                return true;
            }


            /**
             * Called when the query text is changed by the user.
             *
             * @param newText the new content of the query text field.
             * @return false if the SearchView should perform the default action of showing any
             *         suggestions if available, true if the action was handled by the listener.
             */
            @Override
            public boolean onQueryTextChange(String newText) {

                ThizLog.d(TAG, "Inside onQueryTextChange");
                // called when the action bar search text has changed.  Update
                // the search filter, and restart the loader to do a new query
                // with this filter.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
                // Don't do anything if the filter hasn't actually changed.
                // Prevents restarting the loader when restoring state.
                if (mCurFilter == null && newFilter == null) {
                    return true;
                }
                if (mCurFilter != null && mCurFilter.equals(newFilter)) {
                    return true;
                }
                mCurFilter = newFilter;
                getLoaderManager().restartLoader(0, null, this);
                return true;
            }
        });

この特定のケースでは、ユーザーが入力したテキストをCursorLoaderに渡し、適切な結果でカーソルをリロードさせる必要があります。

于 2013-02-03T15:32:45.967 に答える
2

この問題は、ActionBarSherlockのバージョン4.3.0で修正されました。

于 2013-05-02T00:09:26.837 に答える