Kotlin使用高阶函数优化代码

高阶函数定义:参数有函数类型 或者 返回值是函数类型 的函数。

1、在 Kotlin 中定义一个接受函数作为参数的函数:

// 定义一个接受函数类型参数的高阶函数
fun executeFunction(function: () -> Unit) {
    // 在这个函数中执行传入的函数
    function()
}

// 定义一个普通的函数
fun sayHello() {
    println("Hello, World!")
}

fun main() {
    // 将 sayHello 函数作为参数传递给 executeFunction 函数
    executeFunction(::sayHello)
}

2、使用高阶函数实现PopupWindow弹窗效果:

使用高阶函数实现PopupWindow弹窗效果:

1、布局文件:view_popup_drop.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data></data>
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent">
        <ImageView
            android:id="@+id/arr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_top_arrow"
            android:layout_marginEnd="26dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_popup"
            android:fitsSystemWindows="false"
            android:paddingTop="25dp"
            android:paddingBottom="12dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/arr">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tv_start_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/start_time"
                android:textColor="@color/color_text_type"
                android:textSize="14sp"
                app:layout_constraintEnd_toStartOf="@+id/view_left"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/time_tag_start"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:gravity="center"
                android:lines="2"
                android:textColor="@color/tab"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="@+id/tv_start_title"
                app:layout_constraintStart_toStartOf="@+id/tv_start_title"
                app:layout_constraintTop_toBottomOf="@+id/tv_start_title"
                tools:text="2021-09-06\n19:00" />

            <View
                android:id="@+id/view_left"
                android:layout_width="24dp"
                android:layout_height="1dp"
                android:layout_marginTop="6dp"
                android:layout_marginEnd="6dp"
                android:background="@color/chart_grid"
                app:layout_constraintEnd_toStartOf="@+id/divider"
                app:layout_constraintTop_toBottomOf="@+id/tv_start_title" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/divider"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/to"
                android:textColor="@color/color_text_divider"
                app:layout_constraintBottom_toBottomOf="@+id/view_left"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/view_left" />

            <View
                android:id="@+id/view_right"
                android:layout_width="24dp"
                android:layout_height="1dp"
                android:layout_marginStart="6dp"
                android:layout_marginTop="6dp"
                android:background="@color/chart_grid"
                app:layout_constraintStart_toEndOf="@+id/divider"
                app:layout_constraintTop_toBottomOf="@+id/tv_end_title" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tv_end_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="6dp"
                android:gravity="center"
                android:text="@string/end_time"
                android:textColor="@color/color_text_type"
                android:textSize="14sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/view_right"
                app:layout_constraintTop_toTopOf="@+id/tv_start_title" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/time_tag_end"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:gravity="center"
                android:lines="2"
                android:textColor="@color/tab"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="@+id/tv_end_title"
                app:layout_constraintStart_toStartOf="@+id/tv_end_title"
                app:layout_constraintTop_toBottomOf="@+id/tv_end_title"
                tools:text="2021-09-06\n19:00" />
            <Button
                android:id="@+id/reSet"
                android:layout_width="0dp"
                android:layout_height="38dp"
                android:layout_marginTop="16dp"
                android:layout_marginStart="20dp"
                android:background="@drawable/bg_reset"
                android:text="@string/button_reset"
                android:textColor="#666666"
                android:textSize="14sp"
                app:layout_constraintEnd_toStartOf="@+id/post"
                app:layout_constraintStart_toStartOf="@+id/time_tag_start"
                app:layout_constraintTop_toBottomOf="@+id/time_tag_start" />

            <Button
                android:id="@+id/post"
                android:layout_width="0dp"
                android:layout_height="38dp"
                android:layout_marginStart="9dp"
                android:layout_marginEnd="20dp"
                android:background="@drawable/bg_search"
                android:elevation="0dp"
                android:text="@string/button_select"
                android:textColor="@android:color/white"
                android:textSize="14sp"
                app:layout_constraintEnd_toEndOf="@+id/time_tag_end"
                app:layout_constraintStart_toEndOf="@+id/reSet"
                app:layout_constraintTop_toTopOf="@+id/reSet" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

2、封装自定义的控件:

  • 传参为代码块;
  • 内部通过.invoke()执行代码块;
class DropPopupWindow(context: Context?) : PopupWindow(context) {

    private var _binding: ViewPopupDropBinding

    init {
        height = ViewGroup.LayoutParams.WRAP_CONTENT
        width = ViewGroup.LayoutParams.MATCH_PARENT
        isOutsideTouchable = true
        isFocusable = true
        setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        val view = LayoutInflater.from(context).inflate(R.layout.view_popup_drop, null, false)
        _binding = ViewPopupDropBinding.bind(view)
        contentView = _binding.root

        val absoluteSizeSpan = AbsoluteSizeSpan(12.sp, false)
        val startHint = SpannableString(getString(R.string.alarm_start_time))
        startHint.setSpan(absoluteSizeSpan, 0, startHint.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        _binding.timeTagStart.hint = startHint

        val endHint = SpannableString(getString(R.string.alarm_end_time))
        endHint.setSpan(absoluteSizeSpan, 0, endHint.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        _binding.timeTagEnd.hint = endHint
    }

    fun setStartTimePick(listener: (view: AppCompatTextView) -> Unit) {
        _binding.apply {
            timeTagStart.setOnClickListener { listener.invoke(timeTagStart) }
        }
    }

    fun setEndTimePick(listener: (view: AppCompatTextView) -> Unit) {
        _binding.apply {
            timeTagEnd.setOnClickListener { listener.invoke(timeTagEnd) }
        }
    }

    fun reSet(listener: () -> Unit) {
        _binding.apply {
            reSet.setOnClickListener {
                timeTagStart.text = ""
                timeTagEnd.text = ""
                listener.invoke()
                dismiss()
            }
        }
    }

    fun onConfirm(listener: () -> Unit) {
        _binding.apply {
            post.setOnClickListener {
                listener.invoke()
                dismiss()
            }
        }
    }
}

使用:

DropPopupWindow(requireContext()).apply {
            setOnDismissListener {
               // 弹窗关闭时
            }
            setStartTimePick {
                // 设置开始时间
            }
            setEndTimePick {
	            //设置结束时间
            }
            reSet {
                //重置
            }
            onConfirm {
               // 确定
            }
        }

3、双冒号操作符:

fun echoDraftData(
        draftData: LocalDraftData,
        cbAddress: (address: String) -> Unit,
        cbTag: (tagList: ArrayList<VideoTagInfo>) -> Unit
    ) {
        Log.d("caowj", "回显草稿数据")
...
       	 cbAddress(it)
         showAddress(it)
         cbTag(it)
        }
    }

调用方法:

// 写法一:
videoPublishModel.echoDraftData(draftData,{},{binding.topicEdit.tagInfoList = it})

// 写法二(双冒号操作符):
videoPublishModel.echoDraftData(draftData,::showAddress) {
    binding.topicEdit.tagInfoList = it
}

private fun showAddress(addressStr: String?) {
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值