1

我想制作一个可打包的数据类来放置捆绑包。这个数据类还包含一个我将调用的函数。但是当我把应用程序放回去时,我遇到了 parcelize 异常。这是我的可打包数据类,

@Parcelize
data class TransactionConfirmData(
val title: String,
@StringRes val cancelBtnLabel: Int
@StringRes val confirmBtnLabel: Int
val confirmElements: Map<String, String>,
val customConfirmDataList: List<CustomTransactionConfirmData> = emptyList(),
val requestMethodId: Int,
@NavigationRes val cancelDestination: Int = 0,
val contentStructure: 
    TransactionBodyContentCaseTransactionBodyContentCase.BASE_CONFIRM,
 @IgnoredOnParcel val onPressFunction: Serializable

) : 可打包{

fun onPressOkey() = onPressFunction as () -> Unit

companion object {
    fun create(
        title: String,
        @StringRes cancelBtnLabel: Int = R.string.bottom_sheet_behavior
        @StringRes confirmBtnLabel: Int = R.string.bottom_sheet_behavior
        confirmElements: Map<String, String>,
        customConfirmDataList: List<CustomTransactionConfirmData> = emptyList(),
        requestMethodId: Int,
        @NavigationRes  cancelDestination: Int = 0,
        contentStructure: TransactionBodyContentCase = 
               TransactionBodyContentCase.BASE_CONFIRM,
        onPressFunction: ()->Unit = {}
    ): TransactionConfirmData = TransactionConfirmData(
        title,
        cancelBtnLabel,
        confirmBtnLabel,
        confirmElements,
        customConfirmDataList,
        requestMethodId,
        cancelDestination,
        contentStructure,
        onPressFunction as Serializable
    )
}

}

但这不起作用。

4

1 回答 1

0

我用下面的监听器类解决了这个问题;

class Listener(val clickAction:()->Unit):Parcelable{
   fun onClick() = clickAction()
}

然后你可以在你的数据类中创建这样的字段;

val onPressOkey:Listener = Listener {}

你也可以称之为onPressOkey

 yourDataClass.onPressOkey.onClick()
于 2022-02-24T06:11:28.113 回答