You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中AutoCompleteTextView自定义适配器后下拉列表底部空白问题

问题原因&修复方案

1. 列表项布局高度配置错误

item_list.xml的根ConstraintLayout使用了match_parent作为高度,每个列表项都会占满整个下拉弹窗的可视高度,总布局高度会随匹配结果数量成倍增加,超出实际内容的区域就会显示为空白,匹配结果越多空白区域越大。
修复方案:将根布局高度改为wrap_content:

<!-- item_list.xml -->
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/margin">
    <!-- 其余内容不变 -->
</androidx.constraintlayout.widget.ConstraintLayout>

2. 自定义Filter未设置count属性

FilterResults需要同时设置values和count两个属性,ArrayAdapter会读取count值计算下拉列表的整体高度,你当前只赋值了values导致高度计算异常。
修复方案:在performFiltering方法中补充count赋值:

// AutoCompleteTextViewAdapter.kt 的 performFiltering 方法
override fun performFiltering(charSequence: CharSequence?): FilterResults {
    val queryString = charSequence?.toString()?.toLowerCase() ?: ""

    val filterResults = FilterResults()
    val resultList = nameList.filter {
        it.name.toLowerCase().contains(queryString) || it.id.toString().contains(queryString)
    }
    filterResults.values = resultList
    filterResults.count = resultList.size // 补充这行
    return filterResults
}

可选优化

加载布局时传入parent参数,保证列表项的布局参数能正确继承父容器配置:

// AutoCompleteTextViewAdapter.kt 的 getView 方法
val view = convertView ?: LayoutInflater.from(c).inflate(layoutResource, parent, false)

内容的提问来源于stack exchange,提问作者JoshSY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 13:21:02