Android中如何使用Kotlin实现带复选框的Exposed Dropdown下拉列表
带复选框的Material Design Exposed Dropdown实现方案(Kotlin)
原生实现方案(无需引入第三方库)
直接用官方Material Components库即可实现,步骤如下:
- 第一步:引入Material Components依赖
在模块级build.gradle的dependencies块中添加:implementation 'com.google.android.material:material:1.11.0'注意:你的应用主题需要继承自
Theme.MaterialComponents系列主题,才能正常显示Material Design样式的组件。 - 第二步:定义主布局文件
用TextInputLayout包裹AutoCompleteTextView实现基础的Exposed Dropdown结构:<com.google.android.material.textfield.TextInputLayout android:id="@+id/menuTextInputLayout" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="选择选项"> <AutoCompleteTextView android:id="@+id/autoCompleteTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:inputType="none"/> </com.google.android.material.textfield.TextInputLayout> - 第三步:定义下拉项布局
新建res/layout/item_dropdown_checkbox.xml,每个项包含复选框和文本:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingHorizontal="16dp" android:paddingVertical="12dp" android:orientation="horizontal" android:gravity="center_vertical"> <CheckBox android:id="@+id/itemCb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:focusable="false"/> <TextView android:id="@+id/itemTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:textSize="16sp"/> </LinearLayout> - 第四步:定义数据结构和自定义适配器
先定义选项数据类:
再实现自定义适配器:data class DropdownOption(val text: String, var isSelected: Boolean = false)class CheckboxDropdownAdapter( context: Context, private val optionList: List<DropdownOption> ) : ArrayAdapter<DropdownOption>(context, 0, optionList) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val itemView = convertView ?: LayoutInflater.from(context) .inflate(R.layout.item_dropdown_checkbox, parent, false) val currentOption = optionList[position] itemView.findViewById<CheckBox>(R.id.itemCb).isChecked = currentOption.isSelected itemView.findViewById<TextView>(R.id.itemTv).text = currentOption.text return itemView } } - 第五步:在Activity/Fragment中绑定逻辑,实现选择和取值
// 初始化选项数据 val optionList = listOf( DropdownOption("选项A"), DropdownOption("选项B"), DropdownOption("选项C"), DropdownOption("选项D") ) // 绑定适配器 val dropdownAdapter = CheckboxDropdownAdapter(this, optionList) binding.autoCompleteTv.setAdapter(dropdownAdapter) // 处理下拉项点击事件 binding.autoCompleteTv.setOnItemClickListener { _, _, position, _ -> // 切换选中状态 optionList[position].isSelected = !optionList[position].isSelected dropdownAdapter.notifyDataSetChanged() // 可选:更新输入框显示已选内容 val selectedText = optionList.filter { it.isSelected }.joinToString("、") { it.text } binding.autoCompleteTv.setText(selectedText, false) } // 获取所有选中项的方法 fun getSelectedOptions(): List<String> { return optionList.filter { it.isSelected }.map { it.text } }
现成第三方库推荐
如果不想自己写适配逻辑,可以直接使用以下成熟的第三方库:
com.github.skydoves:powerspinner:高度自定义的Spinner组件,原生支持多选、复选框模式,自带Material Design样式com.tiper:materialspinner:专门适配Material Design规范的Spinner,内置多选项选择模式,支持自定义下拉项样式com.github.chivorns:smartmaterialspinner:轻量级库,支持多选、搜索、自定义分隔符等常用功能
内容的提问来源于stack exchange,提问作者Tom Darious
相关产品推荐
相关产品推荐

