Last active
October 20, 2017 11:12
-
-
Save donnfelker/3d0a403b59b8f9f6c99f42f2ca08d4ce to your computer and use it in GitHub Desktop.
MVVM - Removing Logic from Your Views with BindingAdapters
This file contains 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
@BindingAdapter(“isVisible”) | |
fun setIsVisible(view: View, isVisible: Boolean) { | |
if (isVislble) { | |
view.visibility = View.VISIBLE | |
} else { | |
view.visibility = View.GONE | |
} | |
} |
This file contains 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 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) | |
} | |
} |
This file contains 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
<TextView | |
android:layout_height="wrap_content" | |
android:layout_width="match_parent" | |
android:visibility="@{post.hasComments ? View.Visible : View.Gone}" /> |
This file contains 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
<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