IoC之手写运行时注入通用事件

系列文章

IoC之手写运行时注入布局
IoC之手写运行时注入控件
IoC之手写运行时注入点击事件
IoC之手写运行时注入通用事件

通用事件注解


@Target(ElementType.ANNOTATION_TYPE) // 本身自己就是注解,还可以作用域在 注解之上
@Retention(RetentionPolicy.RUNTIME) // 运行时期
public @interface OnBaseCommon {

    // todo 事件三要素1 订阅方式  setOnClickListener, setOnLongClickListener  ...
    String setCommonListener();

    // todo 事件三要素2 事件源对象 View.OnClickListener,  View.OnLongClickListener  ...
    Class setCommonObjectListener();

    // todo 事件三要素3 具体执行的方法(消费事件的方法)   onClick(View v) ,  onLongClick(View v)
    String callbackMethod();
}

这个注解是作用在其它注解上的,比如点击,长按,拖拽的注解。

点击注解

// 点击的注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@OnBaseCommon(setCommonListener = "setOnClickListener",
              setCommonObjectListener = View.OnClickListener.class,
              callbackMethod = "onClick")
public @interface OnClickCommon {

    int value();

}

长按注解


// 长按的注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@OnBaseCommon(setCommonListener = "setOnLongClickListener",
              setCommonObjectListener = View.OnLongClickListener.class,
              callbackMethod = "onLongClick")
public @interface OnClickLongCommon {

    int value();

}

拖拽注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@OnBaseCommon(setCommonListener = "setOnDragListener",
             setCommonObjectListener = View.OnDragListener.class,
             callbackMethod = "onDrag")
public @interface OnDragCommon {

    int value();

}

对通用注解的处理

 /**
     * 兼容Android一系列事件,考虑到扩展
     */
    private static void injectEvnent(final Object mainActivityObject) {

        Class<?> mainActivityClass = mainActivityObject.getClass();

        Method[] declaredMethods = mainActivityClass.getDeclaredMethods();

        for (final Method declaredMethod : declaredMethods) { // 遍历Activity的方法
            declaredMethod.setAccessible(true);

            // 以前的方式,这种方式是不可以的,因为这种方式是具体的获取, 不能具体的获取,因为是动态变化的
           /* Click click = declaredMethod.getAnnotation(Click.class);
            OnClickLongCommon onClickLongCommon = declaredMethod.getAnnotation(OnClickLongCommon.class);
            OnClickCommon onClickCommon = declaredMethod.getAnnotation(OnClickCommon.class);*/

            // 只要我们的注解有 @OnBaseCommon,就代表可以使用,必须要包含OnBaseCommon

            // 这是找不到的,因为有多个注解的情况
            /*OnBaseCommon onBaseCommon = declaredMethod.getAnnotation(OnBaseCommon.class);
            if (onBaseCommon == null) {
            }*/

            Annotation[] annotations = declaredMethod.getAnnotations();//  @Deprecated   @OnClickCommon(R.id.bt_t1)
            for (Annotation annotation : annotations) {
                Class<? extends Annotation> annotationType = annotation.annotationType();

                // 寻找是否有 OnBaseCommon
                OnBaseCommon onBaseCommon = annotationType.getAnnotation(OnBaseCommon.class);
                if (onBaseCommon == null) {
                    // 结束本次循环,进入下一个循环
                    Log.d(TAG, "OnBaseCommon == null ");
                    continue;
                }

                // 证明已经找到了 含有OnBaseCommon的注解
                // 获取事件三要素
                String setCommonListener = onBaseCommon.setCommonListener(); // setOnClickListener
                Class setCommonObjectListener = onBaseCommon.setCommonObjectListener(); // View.OnClickListener.class
                String callbackMethod = onBaseCommon.callbackMethod(); // onClick(View v) {}

                // 之前的方式,,由于是动态变化的,不能这样拿,所以才使用反射
                // annotationType.getAnnotation(OnClickLongCommon.class).value();

                // get R.id.bt_t1 == 8865551
                try {
                    Method valueMethod = annotationType.getDeclaredMethod("value");
                    valueMethod.setAccessible(true);
                    int value = (int) valueMethod.invoke(annotation);

                    // 实例化 R.id.bt_t1 得到View
                    // findViewById(8865551);
                    Method findViewByIdMethod = mainActivityClass.getMethod("findViewById", int.class);
                    // View view = findViewById(8865551);
                    Object viewObject = findViewByIdMethod.invoke(mainActivityObject, value);

                    // Method mViewMethod = view.getClass().getMethod("setOnClickListener", View.OnClickListener.class);
                    Method mViewMethod = viewObject.getClass().getMethod(setCommonListener, setCommonObjectListener);

                    // view.setOnClicListener(new View.OnClickListener...)

                    // 动态代理
                    Object proxy = Proxy.newProxyInstance(
                            setCommonObjectListener.getClassLoader(),
                            new Class[]{setCommonObjectListener}, // OnClickListener
                            new InvocationHandler() {
                                @Override
                                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                                    // 执行MainActivity里面的方法
                                    return declaredMethod.invoke(mainActivityObject, null);
                                }
                            });

                    // 狸猫换太子  换成我们的动态代理
                    mViewMethod.invoke(viewObject, proxy);

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }

    }

将多个注解处理封装在一个方法里面调用。

public class InjectTool {

    private static final String TAG = InjectTool.class.getSimpleName();

    public static void inject(Object object) {
        injectSetContentView(object);

        injectBindView(object);

        injectClick(object);

        injectEvnent(object); // 兼容Android一系列事件
    }
}

使用

 // 点击事件
    @Deprecated
    @OnClickCommon(R.id.bt_t1) // 变化的
    private void test111() {
        Toast.makeText(this, "兼容 点击事件 run", Toast.LENGTH_SHORT).show();


        // 我们需要动态变化事件  事件三要素
        Button button = null;

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return false;
            }
        });

        button.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                return false;
            }
        });

        // todo 事件三要素1 订阅方式  setOnClickListener, setOnLongClickListener  ...

        // todo 事件三要素2 事件源对象 View.OnClickListener,  View.OnLongClickListener  ...

        // todo 事件三要素3 具体执行的方法(消费事件的方法)   onClick(View v) ,  onLongClick(View v)
    }

    // 长按事件
    @OnClickLongCommon(R.id.bt_t2)  // 变化的
    private boolean test222() {
        Toast.makeText(this, "兼容 长按事件 run", Toast.LENGTH_SHORT).show();
        return false;
    }

   /* @OnDragCommon(R.id.bt_t3)
    private boolean test3333() {
        Toast.makeText(this, "兼容 test3333 run", Toast.LENGTH_SHORT).show();
        return false;
    }*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值