注:
使用到了 RecyclerView,声明了其对应的list_item.xml和对应的适配器。
使用到了 枚举类作为数据。
除了最后一部分的点击事件的代码需要根据实际需求再进行修改,其余部分的代码都已调通,可直接复用。
一、需求UI图
直接拍了下自己手绘的UI需求图,如下所示。包含一个关闭的按钮,RecyclerView点击选择项,选择项中被选中项显示出一个表示被选中的图片,其余未选的项中该图片隐藏。
二、代码设计步骤
(1)三个弹窗中复用的drawable:dialog_background.xml
(2)radio_button_dialog.xml,其中包含RecyclerView,所以还需要设计对应的radio.button_dialog_item.xml和适配器RadioButtonDialogAdapter.java
(3)RadionButtonDialogFragment.java
(4)DialogUtils工具类中定义对应的showRadioButtonDialogFragment方法
(5)Activity中设置点击事件,复用showRadioButtonDialogFragment方法。
在调用方法之前,先定义了一个VehicleAlertSensitivity枚举类作为数据源。
三、源码
(1)dialog_background.xml
即最初的背景,可设置颜色,圆角等属性,供几个弹窗的xml复用。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="15dp" />
<solid android:color="@color/cardview_shadow_start_color" />
</shape>
(2)radio_button_dialog.xml,其中包含RecyclerView,所以还需要设计对应的radio.button_dialog_item.xml和适配器RadioButtonDialogAdapter.java
先看radio_button_dialog.xml,它包含三个控件,即标题TextView,关闭按钮ImageView,和一个RecyclerView。
代码如下:
<?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"
android:background="@drawable/dialog_background">
<ImageView
android:id="@+id/close"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/close" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="90dp"
android:layout_marginStart="30dp"
android:textSize="26sp"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/close"
app:layout_constraintTop_toTopOf="@+id/close" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>
其中的ImageView中对应的图片即一个关闭图片,从UI切图中放入drawable中即可。
由于使用到了RecyclerView,所以还需要写对应的list_item和适配器。
先写radio_button_dialog_item.xml:
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">