反射父类方法

 /**
     * 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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值