有两个listview时控制滚动视图

时间:2014-02-10 07:36:22

标签: android uiautomator

我正在使用uiautomator打开WiFi开关,然后连接到WiFi AP 我想做的是:

  1. 开启WiFi
  2. 连接至WiFi AP
  3. 但问题是平板电脑中的设置页面有两个列表视图。
    当我打开WiFi时,我需要控制左侧列表视图 之后,我需要控制正确的列表视图以连接到我的WiFi AP。

    我需要滚动浏览所有列表视图项,但我始终无法这样做。
    我确实尝试通过实例(0)&amp ;;指定listview。实例(1),但有时会失败。

    以下是代码:

    public void TurnOn()
    {
        try {
            UiScrollable settingsList = new UiScrollable(new UiSelector().className(android.widget.ListView.class.getName()).instance(0).scrollable(true));
            UiObject btItem = settingsList.getChildByText(new UiSelector().className(android.widget.LinearLayout.class.getName()),"Wi-Fi", true);
            UiObject btSwitch = btItem.getChild(new UiSelector().className(android.widget.Switch.class.getName()));
            if(!btSwitch.isChecked())
            {
                btSwitch.click();
                sleep(3000);
            }
            else System.out.println("Wifi is already enabled");
    
        } catch (UiObjectNotFoundException e) {
            System.out.println("Can't find the Wi-Fi switch");
        }
    }
    public void ConnectAP()//No password Ap
    { 
        UiScrollable wifiList = new UiScrollable(new UiSelector().className(android.widget.ListView.class.getName()).instance(1).scrollable(true));
        if(!wifiList.exists()) wifiList = new UiScrollable(new UiSelector().scrollable(true));
        else System.out.println("Its tablet");
    
        UiSelector selector = new UiSelector().className(android.widget.RelativeLayout.class.getName());
        UiObject btItem = null;
        int maxSearchSwipes = wifiList.getMaxSearchSwipes();
        for (int i = 0; i < maxSearchSwipes && i<10; i++) 
        {
            try {
                btItem = wifiList.getChildByText(selector, "Guest");
                if(btItem.getChildCount()==2) System.out.println("Wifi is already connected to Guest AP or not in range.");
                else {
                    btItem.clickAndWaitForNewWindow();          
                    sleep(3000);
                }
                break;
            } catch (UiObjectNotFoundException e) { //Do nothing    
            }
            for (int j = 0; j < i; j++){
                try {
                    wifiList.scrollForward();
                } catch (UiObjectNotFoundException e) {
                    System.out.println("Can't scrollForward");
                }
            }
        }
        if(btItem == null) System.out.println("Can't find Guest AP.");
    }
    

    大部分时间它都有效,但有时候代码可以控制错误的列表视图 需要一些宝贵的建议。 谢谢大家!

1 个答案:

答案 0 :(得分:0)

我明白了!

更改原始代码

UiScrollable wifiList = new UiScrollable(new UiSelector().className(android.widget.ListView.class.getName()).instance(1).scrollable(true));

UiScrollable wifiList = new UiScrollable(new UiSelector().className(android.widget.ListView.class.getName()).instance(1));

它有效!