MaterialSearchBar

介绍:

又一个Material Design的Search Bar控件。

运行效果:

使用说明:

把这个放在项目级的build.gradle文件中:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

然后把这个放在app级的build.gradle文件中:

'com.github.mancj:MaterialSearchBar:0.1.0'

把SearchBar添加到activity中:

<com.mancj.materialsearchbar.MaterialSearchBar
    app:speechMode="true"
    app:hint="Custom hint"
    app:maxSuggestionsCount="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/searchBar" />

MaterialSarchBar具有以下xml属性:

属性描述
hintSet custom prompt in the search box
speechModeIf set to true, microphone icon will display instead of the search icon.
maxSuggestionsCountSpecifies the maximum number of search queries stored until the activity is destroyed
iconLeftChange left icon
iconRightChange right icon

公共 方法:

  • disableSearch()

  • enableSearch()

  • isSearchEnabled()

  • setHint(CharSequence hint)

  • setSpeechMode(boolean speechMode)

  • setOnSearchActionListener(OnSearchActionListener onSearchActionListener)

  • setLastSuggestions(List suggestions)

  • getLastSuggestions()

  • setMaxSuggestionCount(int maxQuery)

  • setIconLeft(int iconLefttResId)

  • setIconRight(int iconRightResId)

要在activity销毁的时候保存查询记录,使用searchBar.getLastSuggestions()方法,然后使用searchBar.setLastSuggestions(List); 来重新获取,就如下面的例子中演示的那样。

例子

这里是一个使用MaterialSearchBar的简单例子

private List<String> lastSearches;
private MaterialSearchBar searchBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    searchBar = (MaterialSearchBar) findViewById(R.id.searchBar);
    searchBar.setHint("Custom hint");
    searchBar.setSpeechMode(true);
    //enable searchbar callbacks
    searchBar.setOnSearchActionListener(this);
    //restore last queries from disk
    lastSearches = loadSearchSuggestionFromDiks();
    searchBar.setLastSuggestions(list);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    //save last queries to disk
    saveSearchSuggestionToDisk(searchBar.getLastSuggestions());
}
//called when searchbar enabled or disabled
@Override
public void onSearchStateChanged(boolean enabled) {
    String s = enabled ? "enabled" : "disabled";
    Toast.makeText(MainActivity.this, "Search " + s, Toast.LENGTH_SHORT).show();
}
//called when user confirms request
@Override
public void onSearchConfirmed(CharSequence text) {
    startSearch(text.toString(), true, null, true);
}
//called when microphone icon clicked
@Override
public void onSpeechIconSelected() {
    openVoiceRecognizer();
}
已下载
0