如何使用tablayout在viewpager中设置当前标签 [英] How to set current tab in viewpager with tablayout
问题描述
我有一个自定义viewpager(由于某些原因禁用了滑动),并且正在使用tablayout.内容根据选择的选项卡而变化.我想使用浓缩咖啡进行测试: 1)点击一个特定的标签 2)检查选项卡特定页面中的某些数据 我怎样才能做到这一点.我是特浓咖啡的新手
I have a custom viewpager (with swiping disabled for reasons) working with a tablayout. The content changes based on which tab is selected. I want to test this using espresso: 1) Click on a particular tab 2) Check some data in a particular page of the tab How can I do that. I am a novice to espresso
推荐答案
有多种方法,一种简单的方法是通过选项卡标题选择元素,我使用以下代码:
There are several ways of doing this, an easy one is to select the element by the tab title, I use this code:
Matcher<View> matcher = allOf(withText("TAB TITLE"),
isDescendantOfA(withId(R.id.customTab)));
onView(matcher).perform(click());
SystemClock.sleep(800); // Wait a little until the content is loaded
您只需要使匹配器适合您的布局即可. 然后,检查内容.
You only have to adapt the matcher to your layout. Then, check the content.
例如:
onView(withId(R.id.someId)).check(matches(isCompletelyDisplayed()));
或:
onView(withText(R.string.someText)).check(matches(isCompletelyDisplayed()));
You can find examples here in the Specifying a View Matcher section: http://developer.android.com/intl/es/training/testing/ui-testing/espresso-testing.html
如果您使用RecyclerView来显示选项卡的内容,请查看以下内容: ViewPager中的Espresso RecyclerView
If you are using a RecyclerView to show the content of the tabs have a look at this: Espresso RecyclerView inside ViewPager
这篇关于如何使用tablayout在viewpager中设置当前标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!