/** * scroll Vertical */ public void scrollVertical(final ListView listView, Activity activity, final int distance) { if (listView == null) return; activity.runOnUiThread(new Runnable() { //执行自动化测试的时候模拟滑动需要进入UI线程操作 @Override public void run() { invokeMethod(listView, "smoothScrollBy", new Object[]{distance, 0, true}, new Class[]{int.class, int.class, boolean.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) { TAppLog.i("LG", e.getMessage()); // e.printStackTrace(); } catch (SecurityException e) { TAppLog.i("LG", e.getMessage()); // e.printStackTrace(); } } if (method != null) { method.setAccessible(true); try { returnObj = method.invoke(object, params); } catch (IllegalAccessException e) { TAppLog.i("LG", e.getMessage()); } catch (IllegalArgumentException e) { TAppLog.i("LG", e.getMessage()); } catch (InvocationTargetException e) { TAppLog.i("LG", e.getMessage()); } } return returnObj; }
反射父类方法
最新推荐文章于 2023-11-14 20:03:26 发布