Skip to content

Instantly share code, notes, and snippets.

@dominicthomas
Created June 6, 2018 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dominicthomas/1a0d6d7c81eb69e5ad56a62cb7bfd11d to your computer and use it in GitHub Desktop.
Save dominicthomas/1a0d6d7c81eb69e5ad56a62cb7bfd11d 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
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
}
}
@pradeexsu
Copy link

👍 it's helpfull thanks

@kabeersohail
Copy link

Amazing! I wasted more then a day! finally I found it!! Thanks very much

@Bashirkhalil
Copy link

it's really perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment