UiScrollable

作用:滚动控件, 当目标控件存在于屏幕之外时使用
描述: 继承于UiCollection, 使用该控件描述界面滑动列表, 当目标控件存在于可见范围之外时, 可以使用getChild系列方法来
获取, UiScrollable会自动完成滑动操作以遍历列表里的所有元素。

image.png

 

获取列表的字元素

返回值API备注
UiObjectgetChildByDescription(UiSelector childPattern, String text, boolean allowScrollSearch)是否允许滚动查找获取局别UiSelector条件元素集合后再以文本描述条件查找对象
UiObjectgetChildByDescription(UiSelector childPattern, String text)默认滚动获取具备UiSelector条件的元素集合后再以文件描述条件查找对象
UiObjectgetChildByInstance(UiSelector childPattern, int instance)获取具备UISelector条件的子集,再从子集中按照实例筛选想要的元素,不滚动
UiObjectgetChildByText(UiSelector childPattern, String text, boolean allowScrollSearch)是否允许滚动获取具备条件的元素集合再以文本的条件查找对象
UIObjectgetChildByText(UiSelector childPattern, String Text)默认滚动获取具备UISelector条件元素集合后再以文本条件的查找对象
@Test
    public void testDemo() throws UiObjectNotFoundException {
        UiScrollable scroll = new UiScrollable(new UiSelector().className(""));
        UiObject test = scroll.getChildByText(new UiSelector().className(""), "123", true);
        test.click();
    }

例子:封装一个长列表查询。

//封装一个列表查询
public void settingsListItem(String item) throws UiObjectNotFoundException {
    if (mDevice.hasObject(By.textContains(item))) {
        find(By.textContains(item)).clickAndWait(Until.newWindow(), 3000);
    } else {
        UiScrollable findItem = new UiScrollable(new UiSelector().className("android.support.v7.widget.RecyclerView"));
        findItem.scrollTextIntoView(item);
        find(By.textContains(item)).clickAndWait(Until.newWindow(), 3000);
    }
  }

 

获取与设置最大滚动次数量值

返回值API备注
intgetMaxSearchSwipes()获取执行搜索滚动过程中的最大滚动次数,默认数量为30
UiScrollablesetMaxSearchSwipe(int swipes)设置最大可扫动次数
@Test
    public void testDemo() throws UiObjectNotFoundException {
        UiScrollable scroll = new UiScrollable(new UiSelector().className(""));
        scroll.setMaxSearchSwipes(3);
        scroll.getMaxSearchSwipes();
    }

滑动区域校准常量设置与获取

校准常量:滑动操作坐标时的偏移量,用来取偏移的比例

返回值API备注
doublegetSwipDeadZonePercentage()默认常量的值为0.1, 10%
UiScrollablesetSwipDeadPercentage(double swipeDeadZonePercentage)设置一个部件的大小,在滑动时候视为无接触区的百分比
@Test
    public void testDemo() throws UiObjectNotFoundException {
        UiScrollable scroll = new UiScrollable(new UiSelector().className(""));
        double test = scroll.getSwipeDeadZonePercentage();
        System.out.println("滑动区域的百分比" +test);
        scroll.setSwipeDeadZonePercentage(0.15);
    }

向前与向后滚动

返回值API备注
booleanscrollBackward(int steps)自定义步长向后滚动
booleanscrollBackward()默认步长为55向后滑动
booleanscrollForward默认步长为55向前滑动
booleanscrollBackward(int steps)自定义步长向前滚动
@Test
    public void testDemo() throws UiObjectNotFoundException {
        UiScrollable scroll = new UiScrollable(new UiSelector().className(""));
        scroll.setSwipeDeadZonePercentage(0.1);
        scroll.scrollForward(5000);
        scroll.scrollBackward();
    }

image.png

 

滚动到某个对象

返回值API备注
BooleanscrolLIntoview(UiSelector selector)滚动到条件元素所在位置,并且尽量让其居于屏幕中央
BooleanscrollIntoview (uiobject obj)滚动到文本对象所在位置,并且尽量让居于屏幕中央
BooleanscrollDescriptionIntoView(String.text)滚动到描述所在位置,并且尽量让它居于屏幕中央
BooleanscrolLToBeginning(int maxSwipes)滚动到开始位置
BooleanscrolLToBeginning(int maxSwipes, int
steps)自定义扫动次数与步长滚动到开始位置
BooleanscrolLToEnd(int maxSwipes, int steps)自定义扫动次数与步长滚动到结束位置
BooleanscrolLToEnd(int maxSwipes)自定义扫动次数滚动到结束位置
public void testscrollIntoView() throws UiobjectNotFoundExcept
  Uiscrollable scroll=new Uiscrollable(new UiSelector().clasUiSelector selector=new UiSelector().text ("白");
  Uiobject object=new Uiobject(selector);
  //scroll.scrollIntoView(selector);
  //scroll.scrollIntoView(object);
  //scroll.scrollTextIntoView ("白");
  scroll.scrollDescriptionIntoView("照片");

设置滚动方向

返回值API备注
uiscrollablesetAsHorizontalList()设置滚动方向设置为水平滚动
uiscrollablesetAsVerticalList()设置滚动方向设置为纵向滚动
public void testVertical() throws UiobjectNotFoundException{
  Uiscrollable scroll=new Uiscrollabie(new UiSelector().className("");
  scroll.setAsHorizontalList();
  scroll.setAsVerticalList();

}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐