作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 AutoCompleteTextView 有以下适配器:
class CityAutocompleteAdapter(context: Context?)
: ArrayAdapter<City>(context, R.layout.spinner_city),
Filterable {
init {
}
private var resultList: List<City> = arrayListOf()
override fun getCount(): Int {
return resultList.size
}
override fun getItem(position: Int): City = resultList.get(position)
override fun getItemId(position: Int): Long = position.toLong()
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var inflater = LayoutInflater.from(context)
val city = getItem(position)
var convertView = inflater.inflate(R.layout.spinner_city, parent, false)
var name: TextView = convertView.findViewById(R.id.name)
var country: TextView = convertView.findViewById(R.id.subtitle)
var flag: ImageView = convertView.findViewById(R.id.flag)
name.text = city.name
GlideApp.with(context)
.load(city.flag)
.into(flag)
country.text = city.country
return convertView
}
override fun getFilter(): Filter {
var filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
Timber.w("Filtering")
var filterResults = FilterResults()
if (constraint != null) {
var locations = findCities(constraint.toString())
filterResults.values = locations
filterResults.count = locations.size
}
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results != null && results.count > 0) {
resultList = results.values as (List<City>)
notifyDataSetChanged()
} else {
notifyDataSetInvalidated()
}
}
}
return filter
}
/**
* Returns a search result for the given book title.
*/
private fun findCities(bookTitle: String): List<City> {
return synchronizer.waitForAPI(bookTitle)
}
private val synchronizer = Synchronizer()
private inner class Synchronizer {
internal var result: List<City>? = null
private val lock = java.lang.Object()
internal fun waitForAPI(constraint: CharSequence): List<City> = synchronized(lock) {
var repository: CityRepository = CityRepositoryImpl()
repository.getCitiesByName(constraint.toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(onError = {
}, onSuccess = {
// Timber.w("Results for $constraint are $it")
result = it
notifyAPIDone(it)
})
// At this point, control returns here, and the network request is in-progress in a different thread.
try {
// wait() is a Java IPC technique that will block execution until another
// thread calls the same object's notify() method.
lock.wait()
// When we get here, we know that someone else has just called notify()
// on this object, and therefore this.result should be set.
} catch (e: InterruptedException) {
}
return this.result ?: emptyList()
}
internal fun notifyAPIDone(result: List<City>) = synchronized(lock) {
this.result = result
// API result is received on a different thread, via the API callback.
// notify() will wake up the other calling thread, allowing it to continue
// execution in the performFiltering() method, as usual.
lock.notify()
}
}
}
如您所见,我在 performFiltering 行上放置了一个日志行,我得到的是,在我键入(并停止键入)后,它会在我的 logcat 上不停地显示:
07-11 13:41:56.469 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:57.221 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:57.975 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:58.728 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:41:59.481 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:00.232 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:00.985 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:01.738 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:02.491 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:03.243 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
07-11 13:42:03.995 22176-22259/com.myapp.android.dev.debug W/CityAutocompleteAdapter$getFilter$filter: Filtering
即使我关闭应用程序 (onPause),它也会被执行!这怎么可能?如果我在那里有一个 api 调用,它会不停地执行请求,直到应用程序从内存中清除。
编辑:
使用此适配器的 fragment :
class CitySearchFragment : BaseStepImportFragment<ImportViewModel>() {
override fun backButtonPressed() {
goBack()
}
override val viewModelClass: Class<ImportViewModel> = ImportViewModel::class.java
override val layoutId: Int = R.layout.fragment_import_notes
override fun inject() {
var component = (activity as TextImportActivity).component
component.inject(this)
}
override fun loadUp() {
Timber.e("Saying hello within $this, viewModel $viewModel")
setHasOptionsMenu(true)
city_search_combo.threshold = AUTOCOMPLETE_MIN_THRESHOLD
city_search_combo.setAdapter(CityAutocompleteAdapter(context))
city_search_combo.setOnItemClickListener { adapterView, view, i, l ->
val item = adapterView.getItemAtPosition(i) as City
city_search_combo.setText(item.name)
}
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.textcopy_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.text_copy_done -> {
viewModel.sendText(city_search_combo.text.toString(), notes_text.text.toString())
city_search_combo.clearFocus()
notes_text.clearFocus()
goNext()
}
}
return super.onOptionsItemSelected(item)
}
companion object {
val AUTOCOMPLETE_MIN_THRESHOLD = 3
fun newInstance() = CitySearchFragment()
}
}
最佳答案
自己修好了,用的是这个:
class DelayAutoCompleteTextView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AutoCompleteCombo(context, attrs, android.support.v7.appcompat.R.attr.autoCompleteTextViewStyle) {
var autoCompleteDelay: Long = DEFAULT_AUTOCOMPLETE_DELAY
var progressIndicator: ProgressBar? = null
val delayHandler: Handler = DelayHandler(this)
internal class DelayHandler(val textView: DelayAutoCompleteTextView) : Handler() {
override fun handleMessage(msg: Message?) {
var obj = msg?.obj
var text: String = ""
if (obj is SpannableStringBuilder) {
text = obj.toString()
} else if (obj is String) {
text = obj
}
textView.performFiltering(text, msg?.arg1?:0)
}
}
companion object {
val MESSAGE_TEXT_CHANGES = 100
val DEFAULT_AUTOCOMPLETE_DELAY: Long = 750
}
fun setLoadingIndicator(progressBar: ProgressBar) {
var intrinsicsWidth = ContextCompat.getDrawable(context, R.drawable.ic_dropdown)?.intrinsicWidth
?: 0
var paddingRight = progressBar.paddingRight + intrinsicsWidth
progressBar.setPadding(progressBar.paddingLeft,
progressBar.paddingTop,
paddingRight,
progressBar.paddingBottom)
progressIndicator = progressBar
}
override fun performFiltering(text: CharSequence?, keyCode: Int) {
super.performFiltering(text, keyCode)
progressIndicator?.visibility = View.VISIBLE
delayHandler.removeMessages(MESSAGE_TEXT_CHANGES)
delayHandler.sendMessageDelayed(delayHandler.obtainMessage(MESSAGE_TEXT_CHANGES, text), autoCompleteDelay)
}
override fun onFilterComplete(count: Int) {
progressIndicator?.visibility = View.GONE
super.onFilterComplete(count)
}
fun setDelay(millis: Long) {
autoCompleteDelay = millis
}
}
为了延迟请求,它正在后台线程中一遍又一遍地发布消息以进行过滤。
关于android - 一直调用 AutoCompleteTextView 的 performFiltering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51284662/
我的 AutoCompleteTextView 有以下适配器: class CityAutocompleteAdapter(context: Context?) : ArrayAdapter(
我为 AutoCompleteTextView 编写了自定义过滤器。它工作正常,但是一件小事。 performFiltering 的约束在第一个字符上为空。这意味着只有当第二个字符被放入 AutoCo
我正在尝试过滤 SimpleCursorAdapter ListView 但我在过滤时遇到 sql 异常。我试图找到那些名字以用户输入的文本开头的联系人。 String selection = Co
我是一名优秀的程序员,十分优秀!