Thanks to Code Shogun, whose code I adapted to my situation.
Let your activity implementOnClickListener
as usual:
public class SelectFilterActivity extends Activity implements OnClickListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... */
// Gesture detection
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
Attach your gesture listener to all the views you add to the main layout;
// Do this for each view added to the grid
imageView.setOnClickListener(SelectFilterActivity.this);
imageView.setOnTouchListener(gestureListener);
Watch in awe as your overridden methods are hit, both the onClick(View v)
of the activity and the onFling
of the gesture listener.
public void onClick(View v) {
Filter f = (Filter) v.getTag();
FilterFullscreenActivity.show(this, input, f);
}
The post 'fling' dance is optional but encouraged.
From the Android Developer Documentation:
-
px
> Pixels - corresponds to actual pixels on the screen.
-
in
> Inches - based on the physical size of the screen.
> 1 Inch = 2.54 centimeters
-
mm
> Millimeters - based on the physical size of the screen.
-
pt
> Points - 1/72 of an inch based on the physical size of the screen.
-
dp or dip
> Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160
dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
-
sp
> Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference. Note, the Android documentation is inconsistent on what sp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket |
Screen Density |
Physical Size |
Pixel Size |
ldpi |
120 dpi |
0.5 x 0.5 in |
0.5 in * 120 dpi = 60x60 px |
mdpi |
160 dpi |
0.5 x 0.5 in |
0.5 in * 160 dpi = 80x80 px |
hdpi |
240 dpi |
0.5 x 0.5 in |
0.5 in * 240 dpi = 120x120 px |
xhdpi |
320 dpi |
0.5 x 0.5 in |
0.5 in * 320 dpi = 160x160 px |
xxhdpi |
480 dpi |
0.5 x 0.5 in |
0.5 in * 480 dpi = 240x240 px |
xxxhdpi |
640 dpi |
0.5 x 0.5 in |
0.5 in * 640 dpi = 320x320 px |
Unit |
Description |
Units Per Physical Inch |
Density Independent? |
Same Physical Size On Every Screen? |
px |
Pixels |
Varies |
No |
No |
in |
Inches |
1 |
Yes |
Yes |
mm |
Millimeters |
25.4 |
Yes |
Yes |
pt |
Points |
72 |
Yes |
Yes |
dp |
Density Independent Pixels |
~160 |
Yes |
No |
sp |
Scale Independent Pixels |
~160 |
Yes |
No |
More info can be also be found in the Google Design Documentation.
Best Answer
Let's say you generate a bunch of views that are similar. You could set an
OnClickListener
for each view individually:Then you have to create a unique
onClick
method for each view even if they do the similar things, like:This is because
onClick
has only one parameter, aView
, and it has to get other information from instance variables or final local variables in enclosing scopes. What we really want is to get information from the views themselves.Enter
getTag
/setTag
:Now we can use the same OnClickListener for every button:
It's basically a way for views to have memories.