android - 使用 UIAutomator 获取 android.widget.ListView 的所有子级

标签 android listview android-testing android-uiautomator

是否可以使用UIAutomator获取ListView的所有子项?我面临的问题是 listView.getChildCount(); 仅返回 View 中的子项数量。我尝试滚动并添加计数,但它工作不可靠。

UiScrollable listView = new UiScrollable(new UiSelector().scrollable(
        true).className("android.widget.ListView"));
listView.setAsVerticalList();

listView.flingToBeginning(100);

int count = listView.getChildCount();
listView.scrollForward();
count += listView.getChildCount();
Log.d("STATE", "COUNT: " + count);

当需要滚动时,有没有可靠的方法来获取ListView的所有子项?

最佳答案

不幸的是,Uiautomator 只能看到当前屏幕上的对象...如果您想滚动到列表中的特定对象,您可以这样做:

listView.scrollIntoView(uiobjectYourLookingFor);

或者这个:

UiObject uiObjectYouWant = listView.getChild(newUiSelector().resourceId(com.yourappname:id/yourId");

这两个都会滚动到您指定的对象

关于android - 使用 UIAutomator 获取 android.widget.ListView 的所有子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113763/

相关文章:

android - 在 Google Directions API 失效之前缓存路线的最佳 X 分钟是多少?

Android:Google Playstore 的公测

android - ImageView 上的 setPadding 崩溃 Activity

android - onIceCandidate 在 setRemoteDescription 之后永远不会调用 - webrtc - Android

android - 如何停止应用程序执行,直到 AsyncTask 填充 SharedPreferences

android - listview android 在某些设备上滚动,而在其他设备上不滚动

java - 单击时返回整个字符串的 Spannable 字符串

android listview 项目样式?

android - 重建后仪器测试停止工作 : No tests found

android - Mockito 何时开始实际方法调用 Android UI 测试