Reputation: 81
I wanted to make one tabhost and I wanted to add one viewflipper one activity. This activity works with tabhost. I've written but what does not work. Have anybody an idea how can I do that? I hope they understand what I wanted.
I wanted to use in this activitiy of the GestureDetector. I can change the pictures with button but with finger i cant.
Upvotes: 1
Views: 1683
Reputation: 1588
I have made it before. ( https://github.com/vancexu/AimTo/tree/master/src/com/hackingtrace/vancexu AimToActivity.java)
Just implement your own GestureDetector
,then in your tab activity register a OnTouchListener
By the way, I also implement the slide animation, using ViewFlipper
in TabHost
Some code in my TabHostActivity
private static final int SWIPE_MIN_DISTANCE = 180;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
public void onCreate(Bundle savedInstanceState) {
....
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
TabHost tabHost = getTabHost();
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe, tab change to right
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("Gesture ", "right");
if (currentView == maxTabIndex) {
currentView = 0;
} else {
currentView++;
}
viewFlipperBody.setInAnimation(slideLeftIn);
viewFlipperBody.setOutAnimation(slideLeftOut);
viewFlipperBody.setDisplayedChild(currentView);
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("Gesture ", "left");
if (currentView == 0) {
currentView = maxTabIndex;
} else {
currentView--;
}
viewFlipperBody.setInAnimation(slideRightIn);
viewFlipperBody.setOutAnimation(slideRightOut);
viewFlipperBody.setDisplayedChild(currentView);
}
} catch (Exception e) {
// nothing
e.printStackTrace();
}
tabHost.setCurrentTab(currentView);
return false;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
Upvotes: 2