検索オプションが組み込まれた、カスタマイズ可能な素晴らしい検索ダイアログ。
まずプロジェクトの build.gradle ファイルに jitpack を追加します。
allprojects {
repositories {
.. .
maven { url " https://jitpack.io " }
}
}
次に、モジュールの 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 )
SimpleSearchDialogCompat のインスタンスで表示するにはsetLoading(true)
を使用し、非表示にはsetLoading(false)
使用するだけです。
デフォルトの色を変更したい場合は、 colors.xml
または次のように任意の場所でこれらの色をオーバーライドするだけです。
< color name = " searchDialogResultColor " />
< color name = " searchDialogResultHighlightColor " />
私は単純な検索ダイアログにこのレイアウトを使用しましたが、他のものを使用することもできます。もちろん、レイアウトには次の 2 つのビューが必要です。
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 メソッドにあります。カスタム アダプターを作成して、これの代わりに使用することもできます
結果を強調表示するために使用できる 2 つの方法があります。
/*
* 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.