Skip to content

Instantly share code, notes, and snippets.

@samuel22gj
Created March 3, 2020 08:39
Show Gist options
  • Save samuel22gj/e914707bf353d85343e7836d2ffeda39 to your computer and use it in GitHub Desktop.
Save samuel22gj/e914707bf353d85343e7836d2ffeda39 to your computer and use it in GitHub Desktop.
object RecyclerViewUtils {
fun <VH : RecyclerView.ViewHolder> scrollToHolderWithFallback(
recyclerViewMatcher: Matcher<View>,
viewHolderMatcher: Matcher<VH>,
failureViewAction: ViewAction,
maxRetryCount: Int
) {
var count = maxRetryCount
while (count > 0) {
if (scrollToHolderAndReturnResult(recyclerViewMatcher, viewHolderMatcher)) {
break
} else {
onView(recyclerViewMatcher)
.perform(failureViewAction)
}
count--
}
}
private fun <VH : RecyclerView.ViewHolder> scrollToHolderAndReturnResult(
recyclerViewMatcher: Matcher<View>,
viewHolderMatcher: Matcher<VH>
): Boolean {
var result = true
onView(recyclerViewMatcher)
.withFailureHandler { error, viewMatcher -> result = false }
.perform(RecyclerViewActions.scrollToHolder(viewHolderMatcher))
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment