กล่องโต้ตอบการค้นหาที่ยอดเยี่ยมและปรับแต่งได้พร้อมตัวเลือกการค้นหาในตัว
ขั้นแรกให้เพิ่ม 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 ถูกตั้งค่าเป็นจริง มันจะตรวจสอบว่าจำนวนตัวอักษรที่ตรงกันมากกว่าค่า 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.