Skip to content

Instantly share code, notes, and snippets.

@donnfelker
Last active October 20, 2017 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donnfelker/3d0a403b59b8f9f6c99f42f2ca08d4ce to your computer and use it in GitHub Desktop.
Save donnfelker/3d0a403b59b8f9f6c99f42f2ca08d4ce to your computer and use it in GitHub Desktop.
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