title: Espresso Recylerview in ViewPager match multiple views
body: <p><strong>How do I target a Viewpager with a Fragment that implements a RecylerView with an ID matching multiple views?</strong></p>
  
  <p>I have a MainActivity which has a Viewpager. The Viewpager has 5 tabs. In those 5 tabs, I use RecylerViews to load images in each of them.</p>
  
  <p>The RecylerView XML is reused in different Fragments, so when accessing it with Espresso it keeps complaining ID matches multiple views.</p>
  
  <pre><code>&lt;android.support.v7.widget.RecyclerView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/recycler_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/&gt;
  </code></pre>
  
  <p>My RecylerView doesn't load any text, only loads images, so I can't even do a <code>withText("Text here")</code>. I can't use <code>onData()</code> for RecylerViews neither.  </p>
  
  <pre><code>@RunWith(AndroidJUnit4.class)
  @LargeTest
  public class ExampleTest {
  
      @Rule
      public ActivityTestRule&lt;MainActivity&gt; mActivityRule = new ActivityTestRule&lt;&gt;(
              MainActivity.class);
  
      Activity activity = mActivityRule.getActivity();
  
      @Test
      public void ExampleMethod() {
  
          // perform a swipe
          onView(withId(R.id.viewpager)).perform(swipeLeft());  
  
         // try to click on one of the recycler view items.
         // this crashes:
         onView(withId(R.id.recycler_view))
             .perform(RecyclerViewActions.actionOnItemAtPosition(2, click()));
  
      }
  }
  </code></pre>
  
  <p>So it crashes and points out an error</p>
  
  <blockquote>
    <p>android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.example.app:id/recycler_view' matches multiple views in the hierarchy.
        Problem views are marked with '****MATCHES****' below.</p>
  </blockquote>
  
  <p>//List different hierarchies</p>
  
  <pre><code>View Hierarchy:
  
  View Hierarchy:
  +&gt;DecorView{id=-1, visibility=VISIBLE, width=720, height=1280, 
    has-focus=false, has-focusable=true, has-window-focus=true, 
    is-clickable=false, is-enabled=true, is-focused=false, 
    is-focusable=false, is-layout-requested=false, is-selected=false, 
    root-is-layout-requested=false, has-input-connection=false, x=0.0, 
    y=0.0, child-count=1}
  
  //...
  </code></pre>