问题
将数据绑定与自定义 arrayadapter 和 autocompletetextview 一起使用时,下拉列表无法正确显示。似乎发生的是下拉视图在单个项目视图高度已知之前被膨胀,导致下拉视图在最后一个项目被剪裁时不显示所有单个项目,直到用户滚动一点。这种滚动会导致下拉视图扩展以显示所有单个项目。
我认为问题是当我在 arrayadapter 的 getView() 方法中分配绑定值时,但我无法弄清楚为什么会导致这样的问题。
不正确的“通货膨胀”如下所示:
编码
阵列适配器
覆盖了 getView() 函数的自定义 ArrayAdapter 以使用自定义布局。
class ServiceTypeSpinnerAdapter(context: Context, val items: List<ServiceType>) :
ArrayAdapter<ServiceType>(context, 0, items) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding = convertView?.tag as? DropDownListItemBinding ?:
DropDownListItemBinding.inflate(
LayoutInflater.from(context), parent, false
)
binding.serviceType = getItem(position)
binding.root.tag = binding
return binding.root
}
}
我使用 BindingAdapter 在 AutoCompleteTextView 上设置上述 ArrayAdapter,没有什么特别之处,但如果需要该代码或任何其他代码,就这么说吧。
解决方法
如果我不使用数据绑定,并调用 findViewById() 那么下拉列表按预期工作(第二张图片) - 使用标准:
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
...
if (convertView == null) {
...
} else {
...
}
...
findViewById(...).text = ...
...
return view
}
这似乎是一个有效的解决方法,但必须有一种方法可以正确使用数据绑定?任何帮助将不胜感激。