butterknife
butterknife copied to clipboard
Allow @OnTouch receive not only method but instance of View.OnTouchListener
@OnTouch
is sometimes used to implement dragging.
Dragging in turn requires storing dX
and dY
variables in MotionEvent.ACTION_DOWN
as fields.
This breaks encapsulation a little as dX, dY
are needed for dragging only, outer class does not need them.
Of course, one can save them via View.setTag()
, but this solution is a bit obscure.
So proposal is to allow @OnTouch
take not only methods but instances of View.OnTouchListener
.
Current state
private float dX;
private float dY;
@OnTouch(R.id.corner_point_top_left)
boolean onPointTouch(View cornerPoint, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = cornerPoint.getX() - event.getRawX();
dY = cornerPoint.getY() - event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
cornerPoint.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
return true;
default:
return false;
}
}
Proposed state
@OnTouch(R.id.corner_point_top_left)
View.OnTouchListener listener = new View.OnTouchListener() {
private float dX;
private float dY;
@Override
public boolean onTouch(View v, MotionEvent event) {
// same code as in example above
}
};
P.S. Thanks for great library!
This seems fine, yes.
On Sun, Apr 1, 2018, 7:02 AM Alex Shafir [email protected] wrote:
@OnTouch is sometimes used to implement dragging.
Dragging in turn requires storing dX and dY variables in MotionEvent.ACTION_DOWN as fields.
This breaks encapsulation a little as dX, dY are needed for dragging only, outer class does not need them.
Of course, one can save them via View.setTag(), but this solution is a bit obscure.
So proposal is to allow @OnTouch take not only methods but instances of View.OnTouchListener.
Current state
private float dX; private float dY;
@OnTouch(R.id.corner_point_top_left) boolean onPointTouch(View cornerPoint, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: dX = cornerPoint.getX() - event.getRawX(); dY = cornerPoint.getY() - event.getRawY(); return true; case MotionEvent.ACTION_MOVE: cornerPoint.animate() .x(event.getRawX() + dX) .y(event.getRawY() + dY) .setDuration(0) .start(); return true; default: return false; } }
Proposed state
@OnTouch(R.id.corner_point_top_left) View.OnTouchListener listener = new View.OnTouchListener() {
private float dX; private float dY; @Override public boolean onTouch(View v, MotionEvent event) { // same code as in example above } };
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JakeWharton/butterknife/issues/1237, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEeFS15zh_5ddATBR1dFndB1JXTBGks5tkLO9gaJpZM4TCyu8 .
Any traction here?