Java Android UIAutomation获取动态视图的最后一个子对象(UiObject)

Java Android UIAutomation获取动态视图的最后一个子对象(UiObject),java,android,ui-automation,Java,Android,Ui Automation,我需要使用Android的UIAutomation测试套件获取动态视图的“最后一个孩子” 我有以下代码,但无法正常工作: UiSelector viewSelector = new UiSelector().resourceId("conversation_view").childSelector(new UiSelector().index(4)); UiObject conView = new UiObject(viewSelector); //get the last child UiOb

我需要使用Android的UIAutomation测试套件获取动态视图的“最后一个孩子”

我有以下代码,但无法正常工作:

UiSelector viewSelector = new UiSelector().resourceId("conversation_view").childSelector(new UiSelector().index(4));
UiObject conView = new UiObject(viewSelector);
//get the last child
UiObject lastChild = conView.getChild(new UiSelector().index(conView.getChildCount()-1));
除了“lastChild”部分外,其他一切都正常工作。“getChildCount”获得了正确的子对象数量,因此我知道第一部分是检索正确的父对象

它似乎在搜索“所有”子对象,但我只希望它搜索直接子对象

有人想过如何从UiObject中获取最后一个“直接”子对象吗


谢谢

对于那些感兴趣的人。解决方案是创建一个集合,它将为您提供所有子项的计数。从那里,您可以使用“getChildByInstance”获取最后一个子级

下面的代码将执行此操作:

UiSelector viewSelector = new UiSelector().resourceId("conversation_container").childSelector(new UiSelector().scrollable(true));
//get the last child
UiCollection children = new UiCollection(viewSelector);
int childCount = children.getChildCount(new UiSelector().className("android.view.View"));
UiObject lastChild = children.getChildByInstance(new UiSelector().className("android.view.View"), childCount - 1);