Android Robolectric Click RecyclerView Item

androidandroid-recyclerviewrobolectric

Is there any way to simulate a click on a RecyclerView item with Robolectric?

So far, I have tried getting the View at the first visible position of the RecyclerView, but that is always null. It's getChildCount() keeps returning 0, and findViewHolderForPosition is always null. The adapter returns a non-0 number from getItemCount() (there are definitely items in the adapter).

I'm using Robolectric 2.4 SNAPSHOT.

Best Answer

Seems like the issue was that RecyclerView needs to be measured and layed out manually in Robolectric. Calling this solves the problem:

recyclerView.measure(0, 0);
recyclerView.layout(0, 0, 100, 10000);