注解+反射+动态代理实现View点击事件绑定

一、一些感想

其实在工作的过程中,我一直感觉自己的java基础还是很薄弱的,所以不得不重新看看java基础,其实注解在Android应用实在很广泛,它让代码简介,并且解耦,提高了很多开发效率。为了巩固对基础知识的理解,所以干脆使用注解+反射+动态代理实现View的点击事件绑定功能,加深印象。在整个实现过程中要求的知识点还是比较多,首先要熟悉Android View点击事件,当然如果对注解不了解,那也就没法理解注解的注解,最后也就是动态代理了,不过理解了也就运用自如了。

二、具体实现

1,定义View 事件注解类型,在Android 中View的点击事件分为点击和长按两种,定义事件类型也是为后续工作做好铺点:


/**
 * Description:点击事件类型定义 事件分为长按和短按(即点击) 
 * ElementType.ANNOTATION_TYPE对注解进行注解
* Author:Frank
* Date:2020/5/6
* Version:V1.0.0
* Update:
*/ @Target(ElementType.ANNOTATION_TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface EventType { Class listenerType(); String listenerSetter(); }

2,定义点击事件

/**
 * Description:短按事件类型(点击)
* Author:Frank
* Date:2020/5/6
* Version:V1.0.0
* Update:
*/ @EventType(listenerType = View.OnClickListener.class, listenerSetter = "setOnClickListener") @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface OnClick { @IdRes int[] myValue(); }

3,定义长按事件

/**
 * Description:长按事件类型
* Author:Frank
* Date:2020/5/6
* Version:V1.0.0
* Update:
*/ @EventType(listenerType = View.OnLongClickListener.class, listenerSetter = "setOnLongClickListener") @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface OnLongClick { @IdRes int[] myValue(); }

4,获取注解,使用反射和动态代理完成事件绑定

/**
 * Description:使用反射和动态代理实现View点击事件绑定
* Author:Frank
* Date:2020/5/6
* Version:V1.0.0
* Update:
*/ public class InjectViewClickUtil { private static final String TAG = InjectViewClickUtil.class.getSimpleName(); public static void inject(Activity activity) { if (activity == null) { return; } //得到类对象 Class clz = activity.getClass(); //获取类对象中的所有方法 Method[] methods = clz.getDeclaredMethods(); //判断是否存在成员方法 if (methods == null) { return; } //遍历方法 for (Method m : methods) { //获取方法上的所有注解 Annotation[] annotations = m.getAnnotations(); //判断是否存在注解 if (annotations == null) { continue; } //遍历所有注解 for (Annotation a : annotations) { //得到注解类型对象 Class annotationType = a.annotationType(); if (annotationType.isAnnotationPresent(EventType.class)) { //得到具体注解对象 EventType eventType = annotationType.getAnnotation(EventType.class); //取值 Class listenerType = eventType.listenerType(); Log.d(TAG, "inject: " + listenerType); String listenerSetter = eventType.listenerSetter(); try { //得到标记有OnClick 和 OnLongClick 注解的方法 Method valueMethod = annotationType.getDeclaredMethod("myValue"); //得到所有需要点击控件的id 也就是注解value int[] ids = (int[]) valueMethod.invoke(a); //设置权限 m.setAccessible(true); //InvocationHandler接口是proxy代理实例的调用处理程序实现的一个接口, // 每一个proxy代理实例都有一个关联的调用处理程序;在代理实例调用方法时,方法调用被编码分派到调用处理程序的invoke方法 ListenerInvocationHandler invocationHandler = new ListenerInvocationHandler(m, activity); //创建代理对象 最终会回调我们定义注解的方法 Object proxyInstance = Proxy.newProxyInstance(clz.getClassLoader(), new Class[]{listenerType}, invocationHandler); for (int id : ids) { View view = activity.findViewById(id); Method setter = view.getClass().getMethod(listenerSetter, listenerType); setter.invoke(view, proxyInstance); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } static class ListenerInvocationHandler implements InvocationHandler { private Method method; private T target; public ListenerInvocationHandler(Method method, T target) { this.target = target; this.method = method; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return this.method.invoke(target, args); } } }

三、如何使用

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InjectViewClickUtil.inject(this);
    }

    @OnClick(myValue = {R.id.tv1, R.id.tv2})
    public void onClick(View v) {
        if (v.getId() == R.id.tv1) {
            Toast.makeText(this, ((TextView) v).getText(), Toast.LENGTH_SHORT).show();

        }
        if (v.getId() == R.id.tv2) {
            Toast.makeText(this, ((TextView) v).getText(), Toast.LENGTH_SHORT).show();
        }
    }

    @OnLongClick(myValue = {R.id.tv1, R.id.tv2})
    public boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.tv1:
                Log.d(TAG, "onLongClick: 长安了textView1");
                break;
            case R.id.tv2:
                Log.d(TAG, "onLongClick: 长按了textView2");
                break;
        }
        return true;
    }
}

注意事项:Android View长按事件是需要返回值的即代表当前事件是否消费, public boolean
onLongClick(View view) { return false};由于在实现过程中没有注意这一问题,最后运行报错,检查发现我定义了一个void类型方法,导致执行的时候为空类型不对,直接崩溃。

四,总结

java动态代理机制中有两个重要的类和接口InvocationHandler(接口)和Proxy(类),这一个类Proxy和接口InvocationHandler是我们实现动态代理的核心;如果对动态代理不是很了解可以先看看这篇文章(https://blog.csdn.net/yaomingyang/article/details/80981004),由于是在运行是通过反射获取属性,所以注解选择了运行时,关于注解的生命周期可以根据实际业务自行选择。

你可能感兴趣的:(android)