Lets say you want right and left "swipes" or "flings" to be able to flip through the tabs, like pages in a book.
In my project, I am using tabs that have separate activities to fill the content views.
As far as I can tell, the gesture can be detected two ways: place the gesture listener in each activity or from just this one to control them all. I placed code that handles swipes in the main activity page, the one that contains the set up for the tab host, and also sets the content for all the tabs.
Here is what you add to the main java file:
public class GestureScrollTabActivity extends TabActivity implements OnClickListener {
static TabHost mTabHost;
Intent intent;
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;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
[...]
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
mTabHost.setOnClickListener(GestureScrollTabActivity.this);
mTabHost.setOnTouchListener(gestureListener);
} // end onCreate
[...]
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) {
int i = mTabHost.getCurrentTab();
mTabHost.setCurrentTab(i+1);
Toast.makeText(GestureScrollTabActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int i = mTabHost.getCurrentTab();
mTabHost.setCurrentTab(i-1);
Toast.makeText(GestureScrollTabActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
@Override
public void onClick(View v) {
Filter f = (Filter) v.getTag();
// FilterFullscreenActivity.show(this, input, f);
// Toast.makeText(GestureScrollTabActivity.this, "onClick", Toast.LENGTH_SHORT).show();
}
}
static TabHost mTabHost;
Intent intent;
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;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
[...]
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
mTabHost.setOnClickListener(GestureScrollTabActivity.this);
mTabHost.setOnTouchListener(gestureListener);
} // end onCreate
[...]
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) {
int i = mTabHost.getCurrentTab();
mTabHost.setCurrentTab(i+1);
Toast.makeText(GestureScrollTabActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int i = mTabHost.getCurrentTab();
mTabHost.setCurrentTab(i-1);
Toast.makeText(GestureScrollTabActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
@Override
public void onClick(View v) {
Filter f = (Filter) v.getTag();
// FilterFullscreenActivity.show(this, input, f);
// Toast.makeText(GestureScrollTabActivity.this, "onClick", Toast.LENGTH_SHORT).show();
}
}
Resources:
(1) http://stackoverflow.com/questions/937313/android-basic-gesture-detection
No comments:
Post a Comment