Espresso Recyclerview scroll to end

冷暖自知 提交于 2019-12-12 08:33:57

问题


I have an Android application which has a RecyclerView with N elements, and when this RecyclerView reaches to end when scrolling, then more elements are added (so, it's an infinite list which loads data when scroll reached the bottom).

I would like to test this, but I haven't found a way to do this. I use RecyclerViewActions which have scrollToPosition but even if I put the last position, the end is not reached (because the height of each element is high).

Anybody know how could I do this?


回答1:


I use the below to scroll to the bottom of my RecyclerView.

activity = mActivityTestRule.launchActivity(startingIntent);

onView(withId(R.id.recyclerView))
                .perform(RecyclerViewActions.scrollToPosition(activity.recyclerView.getAdapter().getItemCount() - 1));

You'll then have to use idling resources (or Thread.sleep()) to call this again when more data has loaded.




回答2:


you can implement ViewAction. like this:

class ScrollToBottomAction : ViewAction {
override fun getDescription(): String {
    return "scroll RecyclerView to bottom"
}

override fun getConstraints(): Matcher<View> {
    return allOf<View>(isAssignableFrom(RecyclerView::class.java), isDisplayed())
}

override fun perform(uiController: UiController?, view: View?) {
    val recyclerView = view as RecyclerView
    val itemCount = recyclerView.adapter?.itemCount
    val position = itemCount?.minus(1) ?: 0
    recyclerView.scrollToPosition(position)
    uiController?.loopMainThreadUntilIdle()
}
}

and then use it like this:

onView(withId(R.id.recyclerView)).perform(ScrollToBottomAction())



回答3:


I use this ;

 // Get total item of myRecyclerView
RecyclerView recyclerView = mActivityTestRule.getActivity().findViewById(R.id.myRecyclerView);
int itemCount = recyclerView.getAdapter().getItemCount();

Log.d("Item count is ", String.valueOf(itemCount));

// Scroll to end of page with position
onView(withId(R.id.myRecyclerView))
    .perform(RecyclerViewActions.scrollToPosition(itemCount - 1));


来源:https://stackoverflow.com/questions/42915073/espresso-recyclerview-scroll-to-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!