1. xml 使用spinner控件 ? ? ? ? <Spinner ? ? ? ? ? ? android:id="@+id/orientation_spinner" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" /> ?? ??? ??? ? 2. 代码加载spinner控件 ? ? ? ? val spinner = findViewById<Spinner>(R.id.orientation_spinner) 3. 创建adapter(注意setDropDownViewResource) ? ? ? ? val adapter = ArrayAdapter(spinner.context, android.R.layout.simple_spinner_item, ? ? ? ? ? ? arrayOf(HORIZONTAL, VERTICAL))?? ? ? ? ? ? adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) ?? ??? ?spinner.adapter = adapter?? ? 4. spinner 设置选择?? ??? ? ? ? ? ? val initialPosition = adapter.getPosition(orientationToString(orientation)) ? ? ? ? if (initialPosition >= 0) { ? ? ? ? ? ? spinner.setSelection(initialPosition) ? ? ? ? } 5. spinner设置点击响应 ? ? ? ? spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { ? ? ? ? ? ? override fun onItemSelected( ? ? ? ? ? ? ? ? parent: AdapterView<*>, ? ? ? ? ? ? ? ? view: View?, ? ? ? ? ? ? ? ? position: Int, ? ? ? ? ? ? ? ? id: Long ? ? ? ? ? ? ) { ? ? ? ? ? ? ? ? viewPager.orientation = stringToOrientation(parent.selectedItem.toString()) ? ? ? ? ? ? }
? ? ? ? ? ? override fun onNothingSelected(parent: AdapterView<*>?) { ? ? ? ? ? ? ? ? TODO("Not yet implemented") ? ? ? ? ? ? }
? ? ? ? }
(此外,可以获取到spinner选择的项,如getSelectedItemPosition, getSelectedItem等方法) 2~5完整代码:
package com.example.cardwithviewpager2
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.viewpager2.widget.ViewPager2
/**
* 配置spinner显示方向选项,并设置ViewPager2的方方向 (持有Spinner 和 ViewPager2对象,相当于委托管理)
*/
class OrientationController(private val viewPager: ViewPager2, private val spinner: Spinner) {
fun setup(){
val orientation = viewPager.orientation
val adapter = ArrayAdapter(spinner.context, android.R.layout.simple_spinner_item,
arrayOf(HORIZONTAL, VERTICAL))
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
val initialPosition = adapter.getPosition(orientationToString(orientation))
if (initialPosition >= 0) {
spinner.setSelection(initialPosition)
}
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View?,
position: Int,
id: Long
) {
viewPager.orientation = stringToOrientation(parent.selectedItem.toString())
}
override fun onNothingSelected(parent: AdapterView<*>?) {
TODO("Not yet implemented")
}
}
}
private fun stringToOrientation(orientation: String): Int {
return when(orientation) {
HORIZONTAL -> ViewPager2.ORIENTATION_HORIZONTAL
VERTICAL -> ViewPager2.ORIENTATION_VERTICAL
else -> throw IllegalAccessException("orientation $orientation doesn't exit")
}
}
private fun orientationToString(orientation: Int): String {
return when(orientation) {
ViewPager2.ORIENTATION_HORIZONTAL -> HORIZONTAL
ViewPager2.ORIENTATION_VERTICAL -> VERTICAL
else -> throw IllegalAccessException("orientation $orientation doesn't exit")
}
}
companion object{
const val HORIZONTAL = "Horizontal"
const val VERTICAL = "Vertical"
}
}
?
|