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
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