Uma Achanta
Uma Achanta

Reputation: 3729

Espresso:Test swipe to delete item of Recyclerview inside ViewPager

I want to test swipe left to delete, but its triggering ViewPager swipe action , How to resolve this?

 onView(withId(R.id.orders)).perform(
        RecyclerViewActions.actionOnItemAtPosition<ProductItemViewHolder>(
            1,
            swipeLeft()
        )
    )

Upvotes: 0

Views: 1416

Answers (2)

Alesh17
Alesh17

Reputation: 386

For Kotlin and Kakao users:

fun KRecyclerView.swipeDelete(itemIndex: Int) {
  val from = CoordinatesProvider { it.getPointCoordinatesOfView(0.75f, 0.5f) }
  val to = CoordinatesProvider { it.getPointCoordinatesOfView(0f, 0.5f) }
  val swipeAction = GeneralSwipeAction(Swipe.SLOW, from, to, Press.PINPOINT)
  val viewAction = RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(itemIndex, swipeAction)
  view.perform(viewAction)
}

From and To vals I use for case when phone have side actions (e.g. back action).

fun View.getPointCoordinatesOfView(xPercent: Float, yPercent: Float): FloatArray {
  val xy = IntArray(2).apply { getLocationOnScreen(this) }
  val x = xy[0] + (width - 1) * xPercent
  val y = xy[1] + (height - 1) * yPercent
  return floatArrayOf(x, y)
}

If you don't need this you can simply use GeneralLocation.BOTTOM_RIGHT, GeneralLocation.BOTTOM_LEFT, as in @ricocarpe's answer

Upvotes: 2

ricocarpe
ricocarpe

Reputation: 51

test this :

    onView(withId(R.id.rv_recyclerview)).perform(
            RecyclerViewActions.actionOnItemAtPosition(0, new GeneralSwipeAction(
                    Swipe.SLOW, GeneralLocation.BOTTOM_RIGHT, GeneralLocation.BOTTOM_LEFT,
                    Press.FINGER)));

Upvotes: 4

Related Questions