我的项目中有几种重要的自动完成功能。 这是xml
<com.google.android.material.textfield.TextInputLayout
style="@style/AppTheme.TextInputLayout.AutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/mPaisVeiculo"
style="@style/AppTheme.TextInputEditText.AutoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/pais" />
</com.google.android.material.textfield.TextInputLayout
这是样式
<style name="AppTheme.TextInputLayout.AutoComplete" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu">
<item name="android:background">@drawable/info_display_shape</item>
<item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
<item name="android:textColorHint">@color/textColorPrimary</item>
</style>
<style name="AppTheme.TextInputLayout.AutoComplete.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
<item name="android:textColor">@color/textColorPrimary</item>
<item name="android:textSize">14sp</item>
</style>
<style name="AppTheme.TextInputEditText.AutoComplete" parent="Widget.AppCompat.AutoCompleteTextView">
<item name="android:textSize">16sp</item>
<item name="android:inputType">text</item>
<item name="android:textColor">@color/textColorPrimary</item>
<item name="backgroundTint">@color/white</item>
</style>
这是适配器代码
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Filter
import com.google.android.material.textview.MaterialTextView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.util.*
import kotlin.collections.ArrayList
class TrocoRuaACAdapter(context: Context, listaTrocos: MutableList<ModTrocoRua>) :
ArrayAdapter<ModTrocoRua>(context, 0, listaTrocos) {
private val fullLista: List<ModTrocoRua> = ArrayList(listaTrocos)
override fun getFilter(): Filter {
return filtroTarifa
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
if(convertView == null){
return LayoutInflater.from(context).inflate(R.layout.layout_autocomplete_generico, parent, false)
}
val txtViewNome: MaterialTextView = convertView.findViewById(R.id.mGenericLayouTextView)
val itemTrocoRua:ModTrocoRua? = getItem(position)
if(itemTrocoRua != null)
{
txtViewNome.text = itemTrocoRua.descritivo
}
return convertView
}
private val filtroTarifa: Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val suggestions: MutableList<ModTrocoRua> = ArrayList()
if (constraint.isNullOrEmpty()) {
suggestions.addAll(fullLista)
} else {
val filterPattern: String = constraint.toString().toLowerCase(Locale.ROOT).trim()
fullLista.forEach {
if (it.descritivo.toLowerCase(Locale.ROOT).contains(filterPattern)) {
suggestions.add(it)
}
}
}
val results = FilterResults()
results.values = suggestions
results.count = suggestions.size
return results
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
val gson = Gson()
clear()
val resource = gson.toJson(results!!.values)
val listType = object :
TypeToken<ArrayList<ModTrocoRua>>() {}.type
val values: ArrayList<ModTrocoRua> =
gson.fromJson(
resource,
listType
)
addAll(values)
notifyDataSetChanged()
}
override fun convertResultToString(resultValue: Any?): CharSequence {
return (resultValue as ModTrocoRua).descritivo
}
}
}
一切正常,除了当我单击箭头时,仅显示一个建议。
如果再次单击箭头,一切正常。
这是非常奇怪的行为,我找不到解决它的方法。
此外,我想摆脱多余的压力
有没有建议?