RadioButtonDialogFragment(包含RecyclerView)使用总结


注:
使用到了 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">
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值