مربع حوار بحث رائع وقابل للتخصيص مع خيارات بحث مدمجة.
قم أولاً بإضافة jitpack إلى ملف build.gradle لمشاريعك
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 )
ما عليك سوى استخدام 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 على صحيح، فسوف يتحقق مما إذا كان مقدار الحروف المطابقة أكبر من نسبة الدقة المحددة سيتم إضافة البند إلى النتائج
إن الأداة المستخدمة في 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.