Android 实现账号诊断动画效果,逐条检测对应的项目

Dialog中的项目 逐条检测效果:

依赖库:

复制代码
implementation 'com.github.li-xiaojun:XPopup:2.9.19'
implementation 'com.blankj:utilcodex:1.31.1'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.10'

1、item_account_check.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_marginTop="@dimen/dp_10"
    android:layout_height="@dimen/dp_52">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginEnd="@dimen/dp_10"
        android:layout_toStartOf="@id/iv_state"
        android:layout_alignParentStart="true"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@color/gray_333"
        android:textSize="@dimen/sp_28"
        tools:text="@string/app_name" />

    <ImageView
        android:id="@+id/iv_state"
        android:layout_width="@dimen/dp_40"
        android:layout_height="@dimen/dp_40"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        tools:src="@mipmap/ic_launcher" />
</RelativeLayout>

2、实体类

复制代码
data class CheckResultInfo(
        val text: String,
        val value: String,
        var checkState: Int = -1// 检测状态:0 未检测;1检测中;2已检测
)

3、AccountCheckAdapter .kt

复制代码
open class AccountCheckAdapter : BaseQuickAdapter<CheckResultInfo, BaseViewHolder?>(R.layout.item_account_check) {

    override fun convert(helper: BaseViewHolder, item: CheckResultInfo) {
        try {
            val tvWord = helper.getView<TextView>(R.id.tv_title)
            tvWord.text = item.text
            val ivState = helper.getView<ImageView>(R.id.iv_state)
            if (item.checkState < 1) {
                // 未诊断
                ivState.isVisible = false
            } else if (item.checkState == 1) {
                // 正在诊断
                ivState.isVisible = true
                ImageLoader.loadUrl(mContext, R.mipmap.ic_item_checking, ivState)
                tvWord.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                tvWord.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.resources.getDimension(R.dimen.sp_32))
            } else if (item.checkState == 2) {
                // 已诊断
                ivState.isVisible = true
                ImageLoader.loadUrl(mContext, R.mipmap.ic_item_checked, ivState)
                tvWord.typeface = Typeface.DEFAULT_BOLD
                tvWord.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.resources.getDimension(R.dimen.sp_28))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

4、dialog_account_check.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/transparent"
    android:gravity="center"
    android:orientation="vertical">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_white_radius_24"
        android:orientation="vertical">

        <ImageView
            android:layout_width="@dimen/dp_220"
            android:layout_height="@dimen/dp_220"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="@dimen/dp_40"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_account_checking" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/dp_115"
            android:layout_marginTop="@dimen/dp_24"
            android:layout_marginBottom="@dimen/dp_60"
            tools:listitem="@layout/item_account_check" />

    </androidx.appcompat.widget.LinearLayoutCompat>

    <ImageView
        android:id="@+id/iv_close"
        android:layout_width="@dimen/dp_72"
        android:layout_height="@dimen/dp_72"
        android:layout_marginTop="@dimen/dp_35"
        android:src="@mipmap/ic_close_dialog" />

</androidx.appcompat.widget.LinearLayoutCompat>

5、AccountCheckDialog.kt

复制代码
/**
 * 账号诊断
 */
class AccountCheckDialog(
        mContext: Context,
        private val dataList: List<CheckResultInfo>,
        private val checkedCallback: (() -> Unit)? = null,
) : CenterPopupView(mContext) {
    private lateinit var checkAdapter: AccountCheckAdapter
    private val checkTime = 1500L
    private val MSG_WHAT = 1000

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_account_check
    }

    override fun onCreate() {
        super.onCreate()
        initListener()
        startCheck()
    }

    private fun initListener() {
        val rvList = findViewById<RecyclerView>(R.id.rv_list)
        val ivClose = findViewById<ImageView>(R.id.iv_close)

        with(rvList) {
            layoutManager = LinearLayoutManager(context)
            checkAdapter = AccountCheckAdapter()
            adapter = checkAdapter
            checkAdapter.setNewData(dataList)
        }
        com.jr.libbase.extension.setOnClickListener(ivClose) {
            when (this) {
                ivClose -> {
                    mHandler.removeCallbacksAndMessages(null)
                    dismiss()
                }
            }
        }
    }

    private fun startCheck() {
        val currentPos = 0
        checkAdapter.data[currentPos].checkState = 1
        checkAdapter.notifyItemChanged(currentPos)
        mHandler.sendMessageDelayed(Message().apply {
            what = MSG_WHAT
            arg1 = currentPos
        }, checkTime)
    }


    private val mHandler = MyHandler(this)

    private class MyHandler(dialog: AccountCheckDialog?) : Handler() {
        //弱引用持有HandlerActivity , GC 回收时会被回收掉
        private val weakReference: WeakReference<AccountCheckDialog?>

        init {
            weakReference = WeakReference<AccountCheckDialog?>(dialog)
        }

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            val mDialog: AccountCheckDialog = weakReference.get() ?: return

            when (msg.what) {
                mDialog.MSG_WHAT -> {
                    try {
                        var position = msg.arg1

                        Log.d("caowj", "dialog position=$position")
                        if (position < mDialog.dataList.size) {
                            mDialog.checkAdapter.data[position].checkState = 2
                            mDialog.checkAdapter.notifyItemChanged(position)
                            position += 1

                            if (position <= mDialog.dataList.size - 1) {
                                mDialog.checkAdapter.data[position].checkState = 1
                                mDialog.checkAdapter.notifyItemChanged(position)

                                sendMessageDelayed(Message().apply {
                                    what = mDialog.MSG_WHAT
                                    arg1 = position
                                }, mDialog.checkTime)
                            }else{
                                mDialog.checkedCallback?.invoke()
                                mDialog.dismiss()
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
        }
    }
}

6、使用Dialog:

复制代码
    /**
     * 账号诊断Dialog
     */
    private fun showCheckingDialog(list: List<CheckResultInfo>) {
        XPopup.Builder(context)
            .isDestroyOnDismiss(true)
            .dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .asCustom(AccountCheckDialog(requireContext(), list, checkedCallback = {
                Log.d("caowj", "账号诊断完成,查看检测报告")
            })).show()
    }
相关推荐
祖国的好青年34 分钟前
VS Code 搭建 React Native 开发环境(Windows 实战指南)
android·windows·react native·react.js
黄林晴1 小时前
警惕!AGP 9.2 别只改版本号,R8 规则与构建链路全线收紧
android·gradle
小米渣的逆袭1 小时前
Android ADB 完全使用指南
android·adb
儿歌八万首1 小时前
Jetpack Compose Canvas 进阶:结合 animateFloatAsState 让自定义图形动起来
android·动画·compose
zhangphil2 小时前
Android Page 3 Flow读sql数据库媒体文件,Kotlin
android·kotlin
神探小白牙3 小时前
echarts,3d堆叠图
android·3d·echarts
李白的天不白3 小时前
如何项目发布到github上
android·vue.js
summerkissyou19873 小时前
Android-RTC、NTP 和 System Time(系统时间)
android
小书房3 小时前
Kotlin使用体验及理解1
android·开发语言·kotlin
撩得Android一次心动4 小时前
Android Navigation 组件全面讲解
android·jetpack·navigation