JakeWharton / JakeWharton/butterknife

Allow @OnTouch receive not only method but instance of View.OnTouchListener

Open
#1,237 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
25.3k
Forks
4.5k
PR merge metrics
No merged PRs in 30d

Description

`@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**
```java
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**
```java
@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!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing the @OnTouch annotation and the callback handling that currently accepts methods. Determine how an instance of View.OnTouchListener could be supported, then verify that the proposed listener-based dragging example works without outer dX and dY fields; the payload names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.