0

I am trying to get a number of lines, in Espresso, of the TextView from one of the elements from recycler view. This is my macher for it:

 public static TypeSafeMatcher<RecyclerView.ViewHolder> isTextInLines(final int lines) {
    return new TypeSafeMatcher<RecyclerView.ViewHolder>() {
        @Override
        protected boolean matchesSafely(RecyclerView.ViewHolder item) {
            Log.e("Lines", ((TextView) item.itemView.findViewById(R.id.textTextView)).getLineCount() + "");
            return ((TextView) item.itemView.findViewById(R.id.textTextView)).getLineCount() == lines;
        }


        @Override
        public void describeTo(Description description) {
            description.appendText("isTextInLines");
        }
    };
}

The problem is that it prints that all of them has 0 lines. As I understand the problem might be that it is still in drawing phase according to this:

https://stackoverflow.com/a/24035591/6470918

Any idea how to solve it?

PS. This is the code which uses this matcher:

onView(withId(R.id.recyclerView))
            .perform(scrollToHolder(isTextInLines(12)));

1 Answers1

0

Any RecyclerViewAction::scrollTo... binds all adapter items to the in memory ViewHolders and loops over those to find the adapter position of the item that you are interested in and then scrolls to that position. And because of this, these ViewHolders are never laid out.

Unfortunately, to get the number of lines of the TextView it has to be laid out first.

You can work your problem around with normal ViewAssertion like so onView(matcher).check(matches(isTextInLines(12)) or even onView(isTextInLines(12)).check(matches(isDisplayed())

You might need to scroll to the ViewHolder using other matchers that don't require layout pass first, in case the ViewHolder is off the screen.

Be_Negative
  • 4,892
  • 1
  • 30
  • 33