在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Mauker1/MaterialSearchView开源软件地址(OpenSource Url):https://github.com/Mauker1/MaterialSearchView开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):MaterialSearchViewAndroid SearchView based on Material Design guidelines. The MaterialSearchView will overlay a Toolbar or ActionBar as well as display a ListView for the user to show suggested or recent searches. DownloadTo add the MaterialSearchView library to your Android Studio project, simply add the following gradle dependency: implementation 'br.com.mauker.materialsearchview:materialsearchview:1.3.0-rc02' This library is supported with a min SDK of 14. Important note: If you're still using version 1.0.3, it's recommended to upgrade to the latest version as soon as possible. For more information, please see this issue. New version note: MSV 2.0 is now on beta stage, if you wish to test it, get it by using: implementation 'br.com.mauker.materialsearchview:materialsearchview:2.0.0-beta02' Version 2.0 doesn't require the Content Provider setup and had some API changes which will be added to the documentation later on. For more details please take a look at the V_2.0 branch. Important note on V 2.0: Since I'm using Coroutine Actors, which is marked as obsolete, you'll get a lint warning on each class that uses MSV, to get rid of those add this to your app
Once the Actors methods are updated, I'll update the lib as well. SetupBefore you can use this lib, you have to implement a class named Example: MsvAuthority.java package br.com.mauker;
public class MsvAuthority {
public static final String CONTENT_AUTHORITY = "br.com.mauker.materialsearchview.searchhistorydatabase";
} Or if you're using Kotlin: MsvAuthority.kt package br.com.mauker
object MsvAuthority {
const val CONTENT_AUTHORITY: String = "br.com.mauker.materialsearchview.searchhistorydatabase"
} AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<application ... >
<provider
android:name="br.com.mauker.materialsearchview.db.HistoryProvider"
android:authorities="br.com.mauker.materialsearchview.searchhistorydatabase"
android:exported="false"
android:protectionLevel="signature"
android:syncable="true"/>
</application>
</manifest> Proguard note: Some of you might experience some problems with Proguard deleting the authority class, to solve those problems, add the following lines on your proguard file:
UsageTo open the search view on your app, add the following code to the end of your layout: <br.com.mauker.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/> Then, inside your // Activity:
val searchView: MaterialSearchView = findViewById(R.id.search_view)
override fun onBackPressed() {
if (searchView.isOpen) {
// Close the search on the back button press.
searchView.closeSearch()
} else {
super.onBackPressed()
}
} For more examples on how to use this lib, check the sample app code here. Search history and suggestionsYou can provide search suggestions by using the following methods:
It's also possible to add a single suggestion using the following method:
To remove all the search suggestions use:
And to remove a single suggestion, use the following method:
The search history is automatically handled by the view, and it can be cleared by using:
You can also remove both by using the method below:
Modifying the suggestion list behaviorThe suggestion list is based on a If you want to submit the query from the selected suggestion, you can use the snippet below: searchView.setOnItemClickListener { _, _, position, _ ->
// Do something when the suggestion list is clicked.
val suggestion = searchView.getSuggestionAtPosition(position)
searchView.setQuery(suggestion, false)
} If you just want to set the text on the search view text field when the user selects the suggestion, change the second argument from the Styling the ViewYou can change how your MaterialSearchView looks like. To achieve that effect, try to add the following lines to your styles.xml: <style name="MaterialSearchViewStyle">
<style name="MaterialSearchViewStyle">
<item name="searchBackground">@color/white_ish</item>
<item name="searchVoiceIcon">@drawable/ic_action_voice_search</item>
<item name="searchCloseIcon">@drawable/ic_action_navigation_close</item>
<item name="searchBackIcon">@drawable/ic_action_navigation_arrow_back</item>
<item name="searchSuggestionBackground">@color/search_layover_bg</item>
<item name="historyIcon">@drawable/ic_history_white</item>
<item name="suggestionIcon">@drawable/ic_action_search_white</item>
<item name="listTextColor">@color/white_ish</item>
<item name="searchBarHeight">?attr/actionBarSize</item>
<item name="voiceHintPrompt">@string/hint_prompt</item>
<item name="android:textColor">@color/black</item>
<item name="android:textColorHint">@color/gray_50</item>
<item name="android:hint">@string/search_hint</item>
<item name="android:inputType">textCapWords</item>
</style>
</style> Alternatively, you can also style the Search View programmatically by calling the methods:
And add this line on your style="@style/MaterialSearchViewStyle" So it'll look like: <br.com.mauker.materialsearchview.MaterialSearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/MaterialSearchViewStyle"/> InterfacesCurrently there are two interfaces that you can use to instantiate listeners for:
LanguagesThe MaterialSearchView supports the following languages:
Sample GIFMore InfoFor more use cases, and some examples, you can check the sample app. CreditsThis library was created by Maurício Pessoa with contributions from: JCenter version was made possible with help from: This project was inspired by the MaterialSearchView library by krishnakapil. LicenseThe MaterialSearchView library is available under the Apache 2.0 License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论