Android listview垂直滑动指定距离
来源:互联网 发布:unity3d培训多久学完 编辑:程序博客网 时间:2024/05/22 01:32
Android listview垂直滑动指定距离
在写自动化代码的时候对于listview的拖动有时候效果不尽人意,例如robotium自带的滚动操作scrollListToLine,最终的核心就是:
inst.runOnMainSync(new Runnable(){ public void run(){ view.setSelection(lineToMoveTo); } });
那么也就是说这样滚动其实最终是根据listview的item高度决定滚动的距离,那么我想每次只想滚动y距离怎么做。根据android-19的AbsListView中我们会发现一个方法:
/** * Scrolls the list items within the view by a specified number of pixels. * * @param y the amount of pixels to scroll by vertically * @see #canScrollList(int) */ public void scrollListBy(int y) { trackMotionScroll(-y, -y); }
实际上直接调用此方法是可以解决拖动指定listview垂直距离,但是前提是android-19(至于18有没有我不确定,17是没有的),所以通用性不强,不适合大部分平台,但是可以发现方法:
/** * Track a motion scroll * * @param deltaY Amount to offset mMotionView. This is the accumulated delta since the motion * began. Positive numbers mean the user's finger is moving down the screen. * @param incrementalDeltaY Change in deltaY from the previous event. * @return true if we're already at the beginning/end of the list and have nothing to do. */ boolean trackMotionScroll(int deltaY, int incrementalDeltaY)
从android-10(再之前没有关注)就已经存在了,只不过是相对于包内访问的,那么接下来利用java反射即可调用这个方法,但是有一点需要注意的是因为此方法是ListView的父类的默认修饰符的方法,所以需要反射父类的方法。
/** * scroll Vertical * @param solo * @param y 垂直滑动的距离 */public void scrollVertical(final ListView listView, Activity activity, final int y){ if(listView == null) return; activity.runOnUiThread(new Runnable() { //执行自动化测试的时候模拟滑动需要进入UI线程操作 @Override public void run() { invokeMethod(listView, "trackMotionScroll", new Object[]{-y, -y}, new Class[]{int.class, int.class}); } }); } /** * 遍历当前类以及父类去查找方法,例子,写的比较简单 * @param object * @param methodName * @param params * @param paramTypes * @return */public Object invokeMethod(Object object, String methodName, Object[] params, Class[] paramTypes){ Object returnObj = null; if (object == null) { return null; } Class cls = object.getClass(); Method method = null; for (; cls != Object.class; cls = cls.getSuperclass()) { //因为取的是父类的默认修饰符的方法,所以需要循环找到该方法 try { method = cls.getDeclaredMethod(methodName, paramTypes); break; } catch (NoSuchMethodException e) {//e.printStackTrace(); } catch (SecurityException e) {//e.printStackTrace(); } } if(method != null){ method.setAccessible(true); try { returnObj = method.invoke(object, params); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } return returnObj; }
以上就是整个比较重要的代码,若要结合robotium使用,则可以写的传参更少些:
public void scrollVertical(final ListView listView, Activity activity, final int y){.....}写成:public void scrollVertical(Solo solo, final int y){ listViews = solo.xxx()//根据实际获取listview情况决定 solo.getCurrentActivity().runOnUiThread(new Runnable() { @Override public void run() { invokeMethod(listView, "trackMotionScroll", new Object[]{-y, -y}, new Class[]{int.class, int.class}); } }); }
使用示例:
这里指定y为动态获取设备屏幕的高度:height/n,可以比较灵活的实现滚动操作(例如滚动一屏或者半屏)。
若有错误,欢迎指正!
0 0
- Android listview垂直滑动指定距离
- Android listview垂直滑动指定距离
- Android listview指定垂直滑动距离
- Android - ListView滑动指定tiem和滑动固定距离
- android listview 计算滑动距离
- android 监听listview垂直滚动距离
- 获取Listview滑动距离
- ListView监听滑动距离
- listview滑动距离
- android listview 滑动到指定位置
- Android ListView 滑动到指定item 定位 指定位置
- Android RecyclerView滑动固定距离 滚动指定长度
- android 标题动态变换(listview scrollview 滑动距离计算)
- Android(UI篇) 获取ListView的滑动距离
- android Listview头部带控件滑动距离的判断
- Android 垂直滑动VerticalViewPager
- Android 垂直滑动ViewPager实现
- listview得到滑动距离像素的方法
- 设置屏幕亮度的办法
- Windows下的第一个Spark程序 HelloWorld
- webapi与wcf区别
- 手机触屏版页面开发总结
- 泛型的第二个例子
- Android listview垂直滑动指定距离
- android Activity透明背景
- lua 读写文件
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
- char *s 和 char s[] 的区别小结
- bootstrap 2.3版与3.0版的使用区别
- js数组的操作
- stm32_i/o口操作——01
- 全排列