一個很棒的、可自訂的搜尋對話框,帶有內建的搜尋選項。
首先將 jitpack 加入您的專案 build.gradle 檔案中
allprojects {
repositories {
.. .
maven { url " https://jitpack.io " }
}
}
然後在modules build.gradle檔案中加入依賴
dependencies {
implementation ' com.github.mirrajabi:search-dialog:1.2.4 '
}
如果您只想先使用簡單的搜尋對話框,則需要提供可搜尋的項目。為了實現這一點,您應該在模型中實作 Searchable。
您可以看到 SampleSearchModel 例如:
public class SampleSearchModel implements Searchable {
private String mTitle ;
public SampleSearchModel ( String title ) {
mTitle = title ;
}
@ Override
public String getTitle () {
return mTitle ;
}
public SampleSearchModel setTitle ( String title ) {
mTitle = title ;
return this ;
}
}
現在在您的活動中產生一些搜尋選項:
private ArrayList < SampleSearchModel > createSampleData (){
ArrayList < SampleSearchModel > items = new ArrayList <>();
items . add ( new SampleSearchModel ( "First item" ));
items . add ( new SampleSearchModel ( "Second item" ));
items . add ( new SampleSearchModel ( "Third item" ));
items . add ( new SampleSearchModel ( "The ultimate item" ));
items . add ( new SampleSearchModel ( "Last item" ));
items . add ( new SampleSearchModel ( "Lorem ipsum" ));
items . add ( new SampleSearchModel ( "Dolor sit" ));
items . add ( new SampleSearchModel ( "Some random word" ));
items . add ( new SampleSearchModel ( "guess who's back" ));
return items ;
}
那麼您只需在要顯示對話方塊的位置新增以下行:
new SimpleSearchDialogCompat ( MainActivity . this , "Search..." ,
"What are you looking for...?" , null , createSampleData (),
new SearchResultListener < SampleSearchModel >() {
@ Override
public void onSelected ( BaseSearchDialogCompat dialog ,
SampleSearchModel item , int position ) {
// If filtering is enabled, [position] is the index of the item in the filtered result, not in the unfiltered source
Toast . makeText ( MainActivity . this , item . getTitle (),
Toast . LENGTH_SHORT ). show ();
dialog . dismiss ();
}
}). show ();
建構函數參數是
SimpleSearchDialogCompat ( Context context , String title , String searchHint ,
@ Nullable Filter filter , ArrayList < T > items ,
SearchResultListener < T > searchResultListener )
只需使用setLoading(true)
來顯示它,並setLoading(false)
來隱藏它在 SimpleSearchDialogCompat 的實例上
如果您想更改預設顏色,只需在您的colors.xml
或您想要的任何地方覆蓋這些顏色即可。
< color name = " searchDialogResultColor " />
< color name = " searchDialogResultHighlightColor " />
我使用此佈局進行簡單的搜尋對話框,但您可以使用其他任何佈局。當然你的佈局應該有這兩個視圖:
您可以透過建立繼承 BaseSearchDialogCompat 的類別來使用自訂佈局、適配器和搜尋選項,請查看 SimpleSearchDialogCompat 以了解如何完成此操作的範例您應該實作 BaseSearchDialogCompat 方法:
// handle your view with this one
protected abstract void getView ( View view );
// Id of your custom layout
@ LayoutRes protected abstract int getLayoutResId ();
// Id of the search edittext you used in your custom layout
@ IdRes protected abstract int getSearchBoxId ();
// Id of the recyclerview you used in your custom layout
@ IdRes protected abstract int getRecyclerViewId ();
您可以使用自訂篩選器進行文字搜尋。 SimpleSearchDialogCompat 中使用的是 SimpleSearchFilter。它檢查搜尋鍵,如果某個項目和該鍵具有部分完全相同的字母,它將將該項目添加到結果中,並且如果 CheckLCS 設為 true,它將檢查匹配字母的數量是否大於給定的 AccuracyPercentage項目將被添加到結果中
SimpleSearchDialogCompat 中使用的方法非常簡單,儘管它太長了。主要功能在initializeViews方法中。您可以建立自訂適配器並使用它來代替這個適配器
它有兩種方法可以用來突出顯示結果。
/*
* Returns a SpannableString with
* highlighted LCS(Longest Common Subsequence)
* of two strings with the givven color
*/
SpannableStringBuilder highlightLCS ( String text1 , String text2 , int highlightColor );
// Returns the LCS(Longest Common Subsequence) of two strings
String lcs ( String text1 , String text2 )
1.2.4 - Added an option to SimpleSearchDialogCompat so that the dialog cancellation on touching outside the dialog can be customized.
1.2.3 - Changed minSdkVersion to 14. Added getter for the title textview of simple search dialog. Improved results sorting.
1.2.2 - Gradle tools version and dependencies were updated.
1.2.1 - Added an option for changing text color and highlight color of default adapter.
1.2 - Added getter for views in simple search dialog and an option to turn off the auto filtering on search edittext.
1.1.1 - Fixes drawable overriding issue.
1.1 - Added loading feature.