Spinner使用-kotlin
使用Spinner实现下拉列表
布局文件:
<Spinner
android:id="@+id/tvTest"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:layout_marginStart="20dp"/>```
代码运行
override fun initView() {
val stringArray = arrayOf(getString(R.string.home_list), getString(R.string.home_switch_home), getString(R.string.home_new_home))
val arrayAdapter:ArrayAdapter = ArrayAdapter(requireContext(), R.layout.simple_list_item_1,stringArray)
binding.tvTest.adapter = arrayAdapter
binding.tvTest.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(adapterView: AdapterView<*>?, view: View?, position: Int, id: Long) {
when(position){
0 -> RouterPathUtil.navigate(RouterPathUtil.AIMO_DEVICE_LIST)
1 -> RouterPathUtil.navigatePageType(RouterPathUtil.AIMO_DEVICE_LIST, HomeListPageType.SWITCH)
2 -> RouterPathUtil.navigate(RouterPathUtil.AIMO_NEW_HOME)
}
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
// binding.tvTest.setOnClickListener { RouterPathUtil.navigate(RouterPathUtil.AIMO_DEVICE_LIST) }
}```