Android 可以多行多列的單選列表——MultiLineRadioGroup

項目需要用到一個多行多列的單選列表,用RadioGroup組合或者Recyclerview、GridLayout單獨實現的話很是麻煩,所以就自己根據RadioGroup和GridLayout的特性搬了一個。。。怎麼說是搬呢?其實MultiLineRadioGroup繼承於GridLayout,但是裏面實現的代碼都是照搬RadioGroup的。

GitHub地址:https://github.com/zhumj/MultiLineRadioGroupDemo

先來張效果圖:
在這裏插入圖片描述
使用示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.multilineradiogroupdemo.MultiLineRadioGroup
        android:id="@+id/rgTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <RadioButton
            android:id="@+id/rbTest1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_01"/>

        <RadioButton
            android:id="@+id/rbTest2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_02"/>

        <RadioButton
            android:id="@+id/rbTest3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_03"/>

        <RadioButton
            android:id="@+id/rbTest4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_04"/>

        <RadioButton
            android:id="@+id/rbTest5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_05"/>

        <RadioButton
            android:id="@+id/rbTest6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_06"/>

        <RadioButton
            android:id="@+id/rbTest7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_07"/>

        <RadioButton
            android:id="@+id/rbTest8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_08"/>

        <RadioButton
            android:id="@+id/rbTest9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_09"/>

        <RadioButton
            android:id="@+id/rbTest10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_10"/>

        <RadioButton
            android:id="@+id/rbTest11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_11"/>

        <RadioButton
            android:id="@+id/rbTest12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_12"/>

        <RadioButton
            android:id="@+id/rbTest13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_13"/>

        <RadioButton
            android:id="@+id/rbTest14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="@string/text_14"/>

    </com.example.multilineradiogroupdemo.MultiLineRadioGroup>

</androidx.constraintlayout.widget.ConstraintLayout>
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()
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章