내장된 검색 옵션을 갖춘 훌륭하고 사용자 정의 가능한 검색 대화 상자입니다.
먼저 프로젝트 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 " />
간단한 검색 대화 상자에 이 레이아웃을 사용했지만 다른 것을 사용할 수도 있습니다. 물론 레이아웃에는 다음 두 가지 보기가 있어야 합니다.
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에 사용된 것은 너무 길음에도 불구하고 매우 간단합니다. 주요 기능은 초기화 뷰 메소드에 있습니다. 사용자 정의 어댑터를 생성하여 이 어댑터 대신 사용할 수 있습니다.
결과를 강조 표시하는 데 사용할 수 있는 두 가지 방법이 있습니다.
/*
* 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.