项目需要用到一个多行多列的单选列表,用RadioGroup组合或者Recyclerview、GridLayout单独实现的话很是麻烦,所以就自己根据RadioGroup和GridLayout的特性搬了一个。。。怎么说是搬呢?其实MultiLineRadioGroup继承于GridLayout,但是里面实现的代码都是照搬RadioGroup的。
GitHub地址:https://github.com/zhumj/MultiLineRadioGroupDemo
先来张效果图:
使用示例:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rgTest.setOnCheckedChangeListener(object: MultiLineRadioGroup.OnCheckedChangeListener{
override fun onCheckedChanged(group: MultiLineRadioGroup?, checkedId: Int) {
val message = when (checkedId) {
R.id.rbTest1 -> "01"
R.id.rbTest2 -> "02"
R.id.rbTest3 -> "03"
R.id.rbTest4 -> "04"
R.id.rbTest5 -> "05"
R.id.rbTest6 -> "06"
R.id.rbTest7 -> "07"
R.id.rbTest8 -> "08"
R.id.rbTest9 -> "09"
R.id.rbTest10 -> "10"
R.id.rbTest11 -> "11"
R.id.rbTest12 -> "12"
R.id.rbTest13 -> "13"
else -> "14"
}
showToast("选中$message")
}
})
}
private var toast: Toast? = null
private fun showToast(message: String) {
toast?.cancel()
toast = Toast.makeText(this, message, Toast.LENGTH_SHORT)
toast?.show()
}
}