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;
    }*/
發佈了91 篇原創文章 · 獲贊 1 · 訪問量 5553
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章