Create a custom Annotation ButterKnife

梦想的初衷 提交于 2019-12-12 09:07:59

问题


I'm trying to create a custom listener Annotation to ButterKnife but I can't make it work. Here is my Annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
@ListenerClass(
        targetType = "com.maddogs.mymoney.views.CameraImageView",
        setter = "setCameraImageViewListener",
        type = "com.maddogs.mymoney.views.CameraImageViewListener",
        method = @ListenerMethod(
                name = "onCloseClick",
                parameters = {"com.maddogs.mymoney.views.CameraImageView"},
                returnType = "boolean",
                defaultReturn = "true"
        )
)
public @interface OnCloseClick {
    int[] value() default { View.NO_ID };
}

And my CameraImageView:

 @Override
    public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction() != MotionEvent.ACTION_DOWN
            &&(event.getX(0) <= this.getWidth()
            && event.getY(0) >= 0)
            && event.getX(0) >= this.getWidth() - closeBitmap.getWidth()
            && event.getY(0) <= closeBitmap.getHeight()){

        if(listener == null)
            throw new NullPointerException("CameraImageViewListener cannot be null");

        return listener.onCloseClick(this);
    }
    return super.onTouchEvent(event);
}

public void setCameraImageViewListener(CameraImageViewListener listener) {
        this.listener = listener;
    }

public static interface CameraImageViewListener{
    boolean onCloseClick(CameraImageView view);
}

And my project structure:

The listener is always null. What is wrong?? Thanks


回答1:


You need to add it to this list in ButterKnifeProcessor in order for it to be included in the code generation by the annotation processor.

I've been prototyping what it would look like to have support for custom annotations that does not require modifying the library. No timeline on when it will ship.



来源:https://stackoverflow.com/questions/27613784/create-a-custom-annotation-butterknife

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!