Check text for position in recycler

I have a method where I click on my recyclerView position:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)) .perform(RecyclerViewActions.actionOnItemAtPosition<CurrencySelectorItemHolder>(1, ViewActions.click())) 

But how can I just check the position of my text, for example?

+5
source share
2 answers

Change "all" to the desired text:

 Espresso.onView(withId(R.id.recyclerView)) .perform(RecyclerViewActions.actionOnItem( hasDescendant(withText("whatever")), click())); 

UPDATE: this code is for checking text by position:

 onView(withRecyclerView(R.id.recyclerView).atPosition(3)) .check(matches(hasDescendant(withText("whatever")))); public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) { return new RecyclerViewMatcher(recyclerViewId); } 

RecyclerViewMatcher.java

+5
source

if you want to get the position of the text you clicked on, you can use this method in onClickListener in the adapter

 object.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (object.isChecked() == true) { Log.d("1", "onClick: checked " + getAdapterPosition()); int position = getAdapterPosition(); Class yourObject = yourObject.get(position); yourObject.setChecked(true); selectedyourObjectList.add(todo); } else { if(getAdapterPosition()<selectedyourObjectList.size()) { Log.d("1", "onClick: un checked " + getAdapterPosition()); selectedyourObjectList.remove(getAdapterPosition()); yourObjectList.get(getAdapterPosition()).setChecked(false); } } } }); 

you can save it in any integer variable and then you can check the text at this position

Hope this helps

-1
source

Source: https://habr.com/ru/post/1264079/


All Articles