实际根据关键字筛选已有数据
<androidx.appcompat.widget.SearchView
android:id="@+id/svBrowserUrl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toStartOf="@+id/btnBrowserRefresh"
android:clickable="true"
android:focusableInTouchMode="false"
app:searchIcon="@null"
app:closeIcon="@null"
app:iconifiedByDefault="false"
app:queryHint="@string/string_search_and_input"
android:layout_toEndOf="@id/btnHomePaging"
/>
Adapter 实现Filterable接口
override fun getFilter(): Filter {
if (myFilter == null) {
myFilter = MyFilter()
}
return myFilter!!
}
inner class MyFilter : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val list = arrayListOf<HistoryDataEntity>()
constraint?.let {
//当过滤的关键字不为空的时候,把符合条件的数据对象添加到集合中
for (entity: HistoryDataEntity in dataList) {
if (entity.url.contains(constraint)) {
//要匹配的item中的view
list.add(entity)
}
}
}
val filterResults = FilterResults()
filterResults.count = list.size
filterResults.values = list
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
searchList = results?.values as ArrayList<HistoryDataEntity>?
notifyDataSetChanged()
}
}
注意点:
private val dataList: ArrayList<HistoryDataEntity> //原有数据
private var searchList: ArrayList<HistoryDataEntity>? = dataList //筛选后的数据
override fun getItemCount(): Int {
return searchList?.size ?: 0
}
override fun getItemViewType(position: Int): Int {
return searchList?.get(position)?.type ?: 0
}
activity中
val editTextSearch=svBrowserUrl.findViewById<EditText>(R.id.search_src_text)
context?.let {
editTextSearch.setHintTextColor(ContextCompat.getColor(it,R.color.color_9e9e9e))
}
svBrowserUrl?.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
Log.e("111","-----------$query")
if (!TextUtils.isEmpty(query)) {
rvBrowserList.visibility=View.GONE
editTextSearch?.setText(query?:"")
svBrowserUrl?.clearFocus()
KeyBoardUtils.hideKeyboard(svBrowserUrl)
loadUrl(query)
}
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
Log.e("222","-----------$newText")
//如果newText是长度为0的字符串
if (TextUtils.isEmpty(newText)) {
//清除ListView的过滤
browserSearchHistoryListAdapter?.filter?.filter("")
} else {
//使用用户输入的内容对ListView的列表项进行过滤
browserSearchHistoryListAdapter?.filter?.filter(newText)
}
return true
}
})
svBrowserUrl?.setOnQueryTextFocusChangeListener { view, hasFoucs ->
Log.e("999","-----------$hasFoucs")
if (hasFoucs) {
rvBrowserList.visibility=View.VISIBLE
KeyBoardUtils.showKeyboard(svBrowserUrl)
getData()
}
}
browserSearchHistoryListAdapter= BrowserSearchHistoryListAdapter(dataList)
rvBrowserList.layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
rvBrowserList.adapter=browserSearchHistoryListAdapter
browserSearchHistoryListAdapter?.myItemClick = object : BaseAdapter.MyItemClick{
override fun viewClick(position: Int, view: View) {
val historyData = browserSearchHistoryListAdapter?.getSearchList()?.get(position)
if (view.id == R.id.cLHistoryItem) {
Firebase.getInstance(activity).logEvent("HISTORY_CLICK_LINK", "历史记录点击链接")
rvBrowserList.visibility=View.GONE
editTextSearch?.setText(historyData?.url?:"")
svBrowserUrl?.clearFocus()
KeyBoardUtils.hideKeyboard(svBrowserUrl)
loadUrl(historyData?.url)
}
}
}