在使用espresso进行测试的时候,点击一个横向列表的时候会在点击的项目下出现对应的横线。

实现方式是在FrameLayout下放两个TextView,

一个TextView包含下划线,默认是FrameLayout的第一个元素;

一个TextView不包含下划线,默认是FrameLayout的第二个元素;

两个元素大小一致,当选中时,切换1,2两个TextView的属性。

espresso使用的matcher为:

  

Matcher<View> viewLabel = allOf(withClassName(endsWith("TextView")), isNotCoveredByBothers(), withAlpha(1.0f), isDisplayed(), isDescendantOfA(withId(TABS)));

TABS是列表的祖先元素。
查看代码发现 两个TextView 区别就是 Alpha 值不一样,有下划线的 Alpha 值为1.0.(设置这个值为什么会有这样的下划线)。
    /**
* isNotCoveredByBothers
* @return
*/
public static Matcher<View> isNotCoveredByBothers() {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("is displayed on the screen to the user");
} @Override
public boolean matchesSafely(View view) {
return view.getGlobalVisibleRect(new Rect())
&& !isViewCoveredBybrothers(view);
}
};
} /**
* 检查一个包含父控件的view是否被后面的兄弟遮挡
* 检查一个包含父控件的view是否被后面的兄弟遮挡
* @param view
* @return
*/
public static boolean isViewCoveredBybrothers(final View view) {
View currentView = view;
if (currentView.getParent() == null) {
//无父控件,当作未遮挡
return false;
}
if (currentView.getParent() instanceof ViewGroup) {
ViewGroup currentParent = (ViewGroup) currentView.getParent();
// if the parent of view is not visible,return true
if (currentParent.getVisibility() != View.VISIBLE) {
//父控件不可见
return true;
} int start = indexOfViewInParent(currentView, currentParent);
for (int i = start + 1; i < currentParent.getChildCount(); i++) {
Rect viewRect = new Rect();
view.getGlobalVisibleRect(viewRect);
View otherView = currentParent.getChildAt(i);
Rect otherViewRect = new Rect();
otherView.getGlobalVisibleRect(otherViewRect);
// if view intersects its older brother(covered),return true
if (Rect.intersects(viewRect, otherViewRect)) {
return true;
}
}
}
return false;
} /**
* view在parent的index
* @param view
* @param parent
* @return
*/
private static int indexOfViewInParent(View view, ViewGroup parent) {
int index;
for (index = 0; index < parent.getChildCount(); index++) {
if (parent.getChildAt(index) == view) {
break;
}
}
return index;
}

最新文章

  1. Html&lt;a&gt;标签href的困惑记载
  2. MVC 外网 上传 下载 实现方式(一)
  3. SQLSERVER系统视图,系统表,sys.sql_modules视图
  4. linux crontab 文件位置和日志位置
  5. (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service
  6. cmd扩展路径
  7. Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph
  8. python学习第四天第一部分
  9. Eclipse 实现关键字自动补全功能 (转)
  10. Android 开发笔记“Application 理解”
  11. VS编程,C#串口通讯,通过串口读取数据的一种方法
  12. python ftp批量上传文件下载文件
  13. 关于Image创建的内存管理
  14. 使用FormData格式在前后端传递数据
  15. Linux 小知识翻译 - 「NTP」
  16. 数据库sql的in操作,解决in的过多
  17. 内核进程切换 switch_to
  18. gitlab配置push -f 关闭
  19. std::bind常见的坑
  20. ios中文件下载(带缓存)

热门文章

  1. CyclicBarrier和CountDownLatch的使用
  2. Java 循环队列的实现
  3. 软工实践 - 第十九次作业 Alpha 冲刺 (10/10)
  4. pytorch:EDSR 生成训练数据的方法
  5. 哈希URAL 1941 - Scary Martian Word
  6. EF异常:对一个或多个实体的验证失败
  7. J2EE的十三种技术——JDBC
  8. 【bzoj3436】小K的农场 差分约束系统+最长路-Spfa
  9. NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
  10. Ubuntu安装完之后需要做的事情