我刚刚开始Kotlin,所以请你好看:)
我有一个类负责获取一些数据并通知主活动它需要更新其UI。
所以我在我的DataProvider.kt中创建了一个函数:
@media screen and (max-width: 720px) {
::-webkit-scrollbar {
display: none;
}
}
所以我想从我的活动中调用这个,但我找不到合适的语法! :
fun getPeople(fromNetwork: Boolean, results: ((persons: Array<Person>, error: MyError?) -> Unit)) {
// do some stuff stuff
val map = hashMapOf(
"John" to "Doe",
"Jane" to "Smith"
)
var p = Person(map)
val persons: Array <Person> = arrayOf (p)
results(persons, null)
}
我尝试了很多东西,但我只想获取我的数组和我的可选错误,以便我可以更新UI。
目标是在我的数据提供程序中执行异步代码,以便我的活动可以等待它。
有什么想法吗?非常感谢您的帮助。
答案 0 :(得分:4)
这实际上取决于您如何定义回调方法。如果使用独立功能,请使用::
运算符。首先(当然),我应该解释语法:
(//these parenthesis are technically not necessary
(persons: Array<Person>, error: MyError?)//defines input arguments: an Array of Person and a nullable MyError
-> Unit//defines the return type: Unit is the equivalent of void in Java (meaning no return type)
)
因此该方法定义为:
fun callback(persons: Array<CustomObject>, error: Exception?){
//Do whatever
}
你称之为:
DataProvider.getPeople(
true,
results = this::callback
)
但是,如果您使用匿名回调函数,则会略有不同。这也使用lambda:
getPeople(true, results={/*bracket defines a function. `persons, error` are the input arguments*/persons, error -> {
//do whatever
}})
答案 1 :(得分:0)
是的,Kotlin有一种很好的使用回调函数的方式,下面我将向您展示如何使用它们的示例:
.wrapper{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.cube-wrap {
width: 130px;
height: 130px;
-webkit-perspective: 2000px;
-webkit-perspective-origin: 50% -500px;
}
.single-box {
background-size: cover;
display: block;
position: absolute;
width: 130px;
height: 130px;
background-color: #60c2ef;
-webkit-transform: rotateY(45deg) translateZ(-200px) rotateX(15deg);
-webkit-transform-origin: 50% 50% 0;
}
.box {
-webkit-transform-style: preserve-3d;
-webkit-animation: rotate 1.5s infinite ease;
}
.side-front {
animation: side-front-animation 3s ease infinite;
animation-delay: 100ms;
transform: rotateY(0deg) translateZ(300px);
animation-fill-mode: forwards;
transform-origin: 50% 50%;
background-color: #007dc5;
-webkit-transform: translateZ(20px);
}
.side-back {
animation: side-back-animation 3s ease infinite;
animation-delay: 100ms;
transform: rotateY(-180deg) translateZ(150px);
animation-fill-mode: forwards;
transform-origin: 50% 50%;
background-color: #007dc5;
border: #ffffff;
-webkit-transform: rotateY(180deg) translateZ(20px);
}
.side-top {
animation: side-top-animation 3s ease infinite;
animation-delay: 0;
transform: rotateX(90deg) translateZ(150px);
animation-fill-mode: forwards;
transform-origin: 50% 50%;
background-color: #007dc5;
-webkit-transform: rotateX(90deg) translateZ(20px);
}
.side-bottom {
animation: side-bottom-animation 3s ease infinite;
animation-delay: 0;
transform: rotateX(-90deg) translateZ(150px);
animation-fill-mode: forwards;
transform-origin: 50% 50%;
background-color: #007dc5;
-webkit-transform: rotateX(-90deg) translateZ(20px);
}
.side-left {
animation: side-left-animation 3s ease infinite;
animation-delay: 100ms;
transform: rotateY(-90deg) translateZ(150px);
animation-fill-mode: forwards;
transform-origin: 50% 50%;
background-color: #007dc5;
-webkit-transform: rotateY(-90deg) translateZ(20px);
}
.side-right {
animation: side-right-animation 3s ease infinite;
animation-delay: 100ms;
transform: rotateY(90deg) translateZ(150px);
animation-fill-mode: forwards;
transform-origin: 50% 50%;
background-color: #007dc5;
-webkit-transform: rotateY(90deg) translateZ(20px);
}
@-webkit-keyframes rotate {
0% { -webkit-transform: rotateY(0); }
100% { -webkit-transform: rotateY(360deg); }
}
@-webkit-keyframes side-top-animation {
0% { opacity: 1; transform: rotateX(90deg) translateZ(150px)}
20% { opacity: 1; transform: rotateX(90deg) translateZ(75px)}
70% { opacity: 1; transform: rotateX(90deg) translateZ(75px) }
90% { opacity: 1; transform: rotateX(90deg) translateZ(150px) }
100% { opacity: 1; transform: rotateX(90deg) translateZ(150px) }
}
@-webkit-keyframes side-bottom-animation {
0% { opacity: 1; transform: rotateX(-90deg) translateZ(150px)}
20% { opacity: 1; transform: rotateX(-90deg) translateZ(75px)}
70% { opacity: 1; transform: rotateX(-90deg) translateZ(75px) }
90% { opacity: 1; transform: rotateX(-90deg) translateZ(150px) }
100% { opacity: 1; transform: rotateX(-90deg) translateZ(150px) }
}
@-webkit-keyframes side-front-animation {
0% { opacity: 1; transform: rotateY(0deg) translateZ(150px)}
20% { opacity: 1; transform: rotateY(0deg) translateZ(75px)}
70% { opacity: 1; transform: rotateY(0deg) translateZ(75px) }
90% { opacity: 1; transform: rotateY(0deg) translateZ(150px) }
100% { opacity: 1; transform: rotateY(0deg) translateZ(150px) }
}
@-webkit-keyframes side-back-animation {
0% { opacity: 1; transform: rotateY(-180deg) translateZ(150px)}
20% { opacity: 1; transform: rotateY(-180deg) translateZ(75px)}
70% { opacity: 1; transform: rotateY(-180deg) translateZ(75px) }
90% { opacity: 1; transform: rotateY(-180deg) translateZ(150px) }
100% { opacity: 1; transform: rotateY(-180deg) translateZ(150px) }
}
@-webkit-keyframes side-left-animation {
0% { opacity: 1; transform: rotateY(-90deg) translateZ(150px)}
20% { opacity: 1; transform: rotateY(-90deg) translateZ(75px)}
70% { opacity: 1; transform: rotateY(-90deg) translateZ(75px) }
90% { opacity: 1; transform: rotateY(-90deg) translateZ(150px) }
100% { opacity: 1; transform: rotateY(-90deg) translateZ(150px) }
}
@-webkit-keyframes side-right-animation {
0% { opacity: 1; transform: rotateY(90deg) translateZ(150px)}
20% { opacity: 1; transform: rotateY(90deg) translateZ(75px)}
70% { opacity: 1; transform: rotateY(90deg) translateZ(75px) }
90% { opacity: 1; transform: rotateY(90deg) translateZ(150px) }
100% { opacity: 1; transform: rotateY(90deg) translateZ(150px) }
}
最后,您可以通过以下代码使用新的回调
<div class="wrapper">
<div class="cube-wrap">
<div class="box">
<div class="single-box side-back"></div>
<div class="single-box side-top"></div>
<div class="single-box side-bottom"></div>
<div class="single-box side-left"></div>
<div class="single-box side-right"></div>
<div class="single-box side-front"></div>
</div>
</div>
</div>
因此,基本上,当我成功地向数据库中添加新行时,我正在向服务调用者调用成功或失败响应。希望这会帮助到某人。