Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MVVM - Removing Logic from Your Views with BindingAdapters
@BindingAdapter(“isVisible”)
fun setIsVisible(view: View, isVisible: Boolean) {
if (isVislble) {
view.visibility = View.VISIBLE
} else {
view.visibility = View.GONE
}
}
class CustomBindingAdatpersTest {
@Test
fun isVisibleShouldBeEasilyControlledWithABoolean() {
val v = View(InstrumentationRegistry.getTargetContext())
setIsVisible(v, true) // visible
assertThat(v.visibility).isEqualTo(View.VISIBLE)
setIsVisible(v, false) // gone
assertThat(v.visibility).isEqualTo(View.GONE)
}
}
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:visibility="@{post.hasComments ? View.Visible : View.Gone}" />
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:isVisible="@{post.hasComments()}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment