问题描述
任何人都能够做到这一点. UiScrollable,swipeLeft和swipeRight似乎对它没有任何影响.我正在使用具有最新API的Nexus 5模拟器.有没有人能够拉它?
Has anyone been able to pull this off. UiScrollable, swipeLeft and swipeRight do not appear to have any affect on it. I am using a Nexus 5 emulator with the latest api. Has anyone been able to pull it off?
推荐答案
TL; DR :使用在 ActionBarDrawerToggle
构造函数
TL;DR: Use the content descriptions set in the ActionBarDrawerToggle
constructor
- 设置导航抽屉时,设置一个
ActionBarDrawerToggle
,其中包含用于打开和关闭的内容描述.
- When you set up your Navigation Drawer, set an
ActionBarDrawerToggle
with content descriptions for opening and closing.
// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";
// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";
private void setUpNavigationDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, CONTENT_DESCRIPTION_OPEN_DRAWER, CONTENT_DESCRIPTION_CLOSE_DRAWER);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
- 在
UiAutomatorTestCase
中,要打开/关闭抽屉,请通过步骤1中定义的打开/关闭内容描述找到UI对象,然后单击它.
- In your
UiAutomatorTestCase
, to open/close the drawer, find the UI object by the open/close content description defined in step 1 and perform a click on it.
// Open drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_OPEN_DRAWER = "Open drawer";
// Close drawer content description for accessibility
private static final String CONTENT_DESCRIPTION_CLOSE_DRAWER = "Close drawer";
private void openNavDrawer() throws UiObjectNotFoundException {
findViewByContentDescription(CONTENT_DESCRIPTION_OPEN_DRAWER).click();
}
private void closeNavDrawer() throws UiObjectNotFoundException {
findViewByContentDescription(CONTENT_DESCRIPTION_CLOSE_DRAWER).click();
}
private UiObject findViewByContentDescription(String description) {
return new UiObject(new UiSelector().description(description));
}
警告:如果您使用导航抽屉的材料设计方法(抽屉在Topbar
顶部打开且在状态栏后面),汉堡"图标将绘制在抽屉后面,从而使closeDrawer()
方法不去工作.解决方法是,只需选择抽屉菜单中打开的部分即可.这将关闭抽屉,并显示打开抽屉之前的那部分.
Warning: If you use the Material design approach for the navigation drawer (where the drawer opens on top of the Topbar
and behind the status bar), the "hamburger" icon will be drawn behind the drawer, making the closeDrawer()
method not to work. As a workaround, you can just select the section that is open in the drawer menu; this closes the drawer and shows the same section that was there before opening it.
这篇关于是否可以在uiautomator中滑动打开/关闭导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!