-
-
Save harryhan24/a52ab4cad9d34b24d1a261497a18a10e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainViewModel { | |
val visible = ObservableBoolean(false) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> | |
<variable | |
name="vm" | |
type="MainViewModel" | |
/> | |
</data> | |
<ImageView | |
android:layout_width="50dp" | |
android:layout_height="50dp" | |
app:viewVisible="@{vm.visible}" | |
/> | |
/layout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ViewModel -> View | |
@BindingAdapter("viewVisible") | |
fun viewVisible(v: View, visible: Boolean) { | |
if (visible) { | |
v.visibility = View.VISIBLE | |
} else { | |
v.visibility = View.GONE | |
} | |
} | |
// Databinding -> ViewModel, viewVisibleAttrChanged.onChange() 가 호출되면 리턴 값이 ViewModel 로 바인딩됨 | |
@InverseBindingAdapter(attribute = "viewVisible", event = "viewVisibleAttrChanged") | |
fun viewVisible(v: View): Boolean { | |
return v.visibility == View.VISIBLE | |
} | |
// View -> Databinding, View 에 변화가 생기면 viewVisibleAttrChanged 이벤트로 등록된 InverseBindingAdapter 함수를 호출 | |
@BindingAdapter("viewVisibleAttrChanged", requireAll = false) | |
fun viewVisibleChanged(v: View, newAttrChanged: InverseBindingListener) { | |
newAttrChanged.onChange() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment