Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type inference failed: RecyclerViewActions.scrollTo()

Tags:

kotlin

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?

like image 328
Josh Laird Avatar asked May 30 '17 17:05

Josh Laird


2 Answers

I needed to add <RecyclerView.ViewHolder> to scrollTo

onView(withId(R.id.recyclerView)).perform(
    RecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(
    hasDescendant(withText(artistResult.nameVariations[0]))))
like image 190
Josh Laird Avatar answered Sep 16 '22 21:09

Josh Laird


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.

like image 32
erluxman Avatar answered Sep 20 '22 21:09

erluxman