When a View is "clicked" the implementation of the View class calls the method onClick()
implementation of the Onclicklistener interface, indicated by the method setOnClickListener()
.
As it is not possible to know beforehand that the button will be clicked, the method beforeClick()
has to be called in the method onClick()
.
How to implement the onClick()
is specified by the user of the class, you need to find a way to have that code executed after executing beforeClick()
and before executing afterClick()
.
That is, when clicking the button, the code to be executed has to be implemented by you, in order to respect that sequence, and not the one indicated by the user, through the method setOnClickListener()
.
So you have to do the override of the method setOnClickListener()
so that your Onclicklistener implementation is used, the one that runs in sequence beforeClick()
, onClick()
and afterClick()
.
private OnClickListener mUserOnClickListener;
private OnClickListener mMyOnClickListener;
private OnBeforeClickListener mOnBeforeClickListener;
private OnAfterClickListener mOnAfterClickListener;
@override
public void setOnClickListener(OnClickListener l) {
mUserOnClickListener = l;
super.setOnClickListener(mMyOnClickListener);
}
The method onClick()
of mMyOnClickListener will be anything like:
@override
public void onClick(View v){
mOnBeforeClickListener.beforeClick(v);
mUserOnClickListener.onClick(v);
mOnAfterClickListener.afterClick(v);
}
Do not forget to check if the listeners are not null.
Thanks. It worked out!
– AlexQL