Using Espresso to click view inside RecyclerView item

后端 未结 9 1912
醉酒成梦
醉酒成梦 2020-12-02 05:36

How can I use Espresso to click a specific view inside a RecyclerView item? I know I can click the item at position 0 using:

onView(withId(R.i

9条回答
  •  -上瘾入骨i
    2020-12-02 06:06

    You can even generalize this approach to support more actions not only click. Here is my solution for this:

    fun  recyclerChildAction(@IdRes id: Int, block: T.() -> Unit): ViewAction {
      return object : ViewAction {
        override fun getConstraints(): Matcher {
          return any(View::class.java)
        }
    
        override fun getDescription(): String {
          return "Performing action on RecyclerView child item"
        }
    
        override fun perform(
            uiController: UiController,
            view: View
        ) {
          view.findViewById(id).block()
        }
      }
     
    }
    

    And then for EditText you can do something like this:

    onView(withId(R.id.yourRecyclerView))
            .perform(
                actionOnItemAtPosition(
                    0,
                    recyclerChildAction(R.id.editTextId) { setText("1000") }
                )
            )
    

提交回复
热议问题