Type inference failed: RecyclerViewActions.scrollTo()

Viewed 4506

From java:

onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollTo(
    hasDescendant(withText(artistResult.getNameVariations().get(0)))));

Trying to convert to Kotlin:

onView(withId(R.id.recyclerView)).perform(RecyclerViewActions.scrollTo(
    hasDescendant(withText(artistResult.nameVariations[0]))))

I get this stacktrace:

Error:(63, 71) Type inference failed: Not enough information to infer parameter VH in fun <VH : RecyclerView.ViewHolder!> scrollTo(itemViewMatcher: Matcher<View!>!): RecyclerViewActions.PositionableRecyclerViewAction!
Please specify it explicitly.

I'm not entirely sure where I can specify "it" explicitly. When this has come up previously it's because I didn't initialise the value correctly, but here I'm calling a method. Any ideas?

4 Answers

One can get easily confused about which VH among many(specially if multiple VHs in a single RecyclerView) to use, you can simply use Generic ViewHolder i.e RecyclerView.ViewHolder like :

RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>

RecyclerViewActions.actionOnHolderItem<RecyclerView.ViewHolder>

RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>

etc.

I hope this helps.

do this: onView(withId(R.id.recycler_view)) .perform(RecyclerViewActions.scrollToPosition(11))

I´m just going to leave my solution here in case anyone needs it.

onView(withId(androidx.preference.R.id.recycler_view)) .perform(actionOnItem<RecyclerView.ViewHolder>(hasDescendant(withText(R.string.settings_reset_app)), click()).atPosition(1));

Related