Android实现带搜索建议的浮动搜索框:Floating Search View

jopen 5年前

实现带搜索建议的浮动搜索框。

Alt text

用法

  1. In your dependencies, addcompile 'com.github.arimorty:floatingsearchview:1.0.1'
  2. Add a FloatingSearchView to your view hierarchy, and make sure that it takes up the full width and height of the screen
  3. Listen to query changes and provide suggestion items that implement SearchSuggestion

示例:

   <com.arlib.floatingsearchview.FloatingSearchView                 android:id="@+id/floating_search_view"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 app:floatingSearch_searchBarMarginLeft="@dimen/search_view_inset"                 app:floatingSearch_searchBarMarginTop="@dimen/search_view_inset"                 app:floatingSearch_searchBarMarginRight="@dimen/search_view_inset"                 app:floatingSearch_showMenuAction="true"                 app:floatingSearch_searchHint="Search..."                 app:floatingSearch_voiceRecHint="Say something..."                 app:floatingSearch_showSearchHintWhenNotFocused="true"                 app:floatingSearch_showVoiceInput="true"                 app:floatingSearch_showOverFlowMenu="true"                 app:floatingSearch_hideOverflowMenuWhenFocused="true"                 app:floatingSearch_showSearchKey="false"                 app:floatingSearch_dismissOnOutsideTouch="true"                 app:floatingSearch_menu="@menu/menu_main"/>

  mSearchView.setOnQueryChangeListener(new FloatingSearchView.OnQueryChangeListener() {                @Override                public void onSearchTextChanged(String oldQuery, final String newQuery) {                      //get suggestions based on newQuery                      //pass them on to the search view                    mSearchView.swapSuggestions(newSuggestions);                }            });

Configure overflow menu:

In your xml, add menu resource

    app:floatingSearch_menu="@menu/menu_main"

Listen for item selections

   mSearchView.setOnMenuItemClickListener(new FloatingSearchView.OnMenuItemClickListener() {        @Override        public void onMenuItemSelected(MenuItem item) {                            }     });

项目主页:http://www.open-open.com/lib/view/home/1448896126248