Filterable
2021. 11. 4. 13:37ㆍAndroid
반응형
class SearchListAdapter(
private val context: Context,
private val callback: (String) -> Unit
) : RecyclerView.Adapter<SearchListAdapter.ViewHolder>(), Filterable {
private var itemList: MutableList<SearchResultItem> = arrayListOf()
private var itemFilteringList: MutableList<SearchResultItem> = arrayListOf()
private var itemFilterResultList: MutableList<SearchResultItem> = arrayListOf()
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterResults = FilterResults()
val words = constraint.toString()
if (words.isNotEmpty()) {
if (itemFilteringList.size > 0) {
itemFilteringList.clear()
}
itemList.forEach {
if (it.contents.indexOf(words) > 0) {
itemFilteringList.add(it)
}
}
filterResults.values = itemFilteringList
} else {
filterResults.values = itemList
}
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
results?.let {
itemFilterResultList = (it.values as MutableList<SearchResultItem>)
notifyDataSetChanged()
}
}
}
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val layoutSearchResult: RelativeLayout = view.findViewById(R.id.layout_search_result)
val resultContents: TextView = view.findViewById(R.id.result_contents)
fun bind(view: View, text: String) {
view.setOnClickListener {
callback(text)
}
}
}
}
https://smartstore.naver.com/byrollin?
반응형
'Android' 카테고리의 다른 글
[전자책] 안드로이드 Compose 기반 암호화폐 거래소 연동 앱 개발 (0) | 2024.05.14 |
---|---|
AppUpdateManager (0) | 2021.10.26 |
Android 11 (Api 30) 파일 저장 관련 (0) | 2021.05.27 |
안드로이드 RecyclerView 성능 개선팁 (0) | 2020.12.10 |
HorizontalScrollView Scroll Animation (0) | 2020.12.10 |