I'm work on a project KBinding(https://github.com/BennyWang/KBinding.git)
which work on the binding between View and ViewModel.
Views are created by Anko DSL like
class MainActivityUI : ViewBinderComponent<MainActivity> {
override fun builder(): AnkoContext<*>.() -> Unit = {
verticalLayout {
verticalLayout {
backgroundColor = Color.WHITE
leftPadding = dip(14)
editText {
hint = "请输入手机号或者电子邮箱地址"
bind { text(path="name", mode = BindingMode.TwoWay) }
}.lparams(width = matchParent)
view { backgroundResource = R.color.color_f2 }.lparams(width =
matchParent, height = 1)
editText {
hint = "请输入密码"
bind{ text(path="password", mode = BindingMode.TwoWay) }
}.lparams(width = matchParent)
}.lparams(width = matchParent)
textView {
text = "登录"
textSizeDimen = R.dimen.font_38
textColor = Color.WHITE
verticalPadding = dip(10.4f)
isClickable = true
bind { click("login") }
bind { enabled(paths=listOf("name", "password"), converter =
ArrayToBooleanConverter()) }
}.lparams(width = matchParent) { margin = dip(14) }.let {
it.gravity = Gravity.CENTER }
}
}
}
ViewModel like this
class LoginViewModel(val delegate: LoginViewModel.LoginDelegate) :
ViewModel<String>() {
var level: Int by Delegates.bindProperty("level", 3)
var name: String by Delegates.bindProperty("name", "[email protected]")
var password: String by Delegates.bindProperty("password", "xxxxxxxxx")
val login: Command by Delegates.bindCommand("login", Command { it ->
if(name.equals("wangbin")) delegate.onLoginSuccess("wangbin")
else delegate.onLoginFailed(RuntimeException("incorrect name or
password"))
})
interface LoginDelegate {
fun onLoginSuccess(s: String)
fun onLoginFailed(e: Throwable)
}
}
once binding, the change of viewmodel property will change the display of
view.
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/8339dc82-778b-48ee-8d62-4b3d1e4f2487%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.