Forked from dominicthomas/MatchChildViewWithText.kt
Created
December 20, 2022 13:47
-
-
Save ankit1057/e247721fce1541018da4e974f2b5f2b3 to your computer and use it in GitHub Desktop.
Use this to iterate through a recycler view item and check each position to see if a view exists that has some specific text set
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
| fun matchChildViewOfAccountTile(accountName: String, targetViewId: Int, itemMatcher: Matcher<View>): Matcher<View> = | |
| object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) { | |
| override fun describeTo(description: Description) { | |
| description.appendText("Has view id $targetViewId and matches $itemMatcher for item with name $accountName") | |
| } | |
| public override fun matchesSafely(recyclerView: RecyclerView): Boolean { | |
| val itemCount = recyclerView.adapter.itemCount | |
| for (i in 0 until itemCount) { | |
| val holder = recyclerView.findViewHolderForAdapterPosition(i) | |
| if (holder != null) { | |
| val accountNameView = holder.itemView.findViewById<View>(R.id.accountTileAccountName) as TextView | |
| if (accountNameView.text == accountName) { | |
| val targetView = holder.itemView.findViewById<View>(targetViewId) | |
| return itemMatcher.matches(targetView) | |
| } | |
| } | |
| } | |
| return false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment