我有一个TabHost控件(不在actionbar中),我想让选项卡在用户滑动每个选项卡上的上下文时改变(类似whatsapp emoji选项卡)。
我该怎么做呢?
编辑
感觉也很重要。我希望上下文应该有滚动动画(无论用户是滑动还是单击选项卡)。
你可以重写onTouchEvent:
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
// when user first touches the screen to swap
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
// if left to right swipe on screen
if (lastX < currentX) {
switchTabs(false);
}
// if right to left swipe on screen
if (lastX > currentX) {
switchTabs(true);
}
break;
}
}
return false;
}
switchTabs方法:
public void switchTabs(boolean direction) {
if (direction) // true = move left
{
if (tabHost.getCurrentTab() == 0)
tabHost.setCurrentTab(tabHost.getTabWidget().getTabCount() - 1);
else
tabHost.setCurrentTab(tabHost.getCurrentTab() - 1);
} else
// move right
{
if (tabHost.getCurrentTab() != (tabHost.getTabWidget()
.getTabCount() - 1))
tabHost.setCurrentTab(tabHost.getCurrentTab() + 1);
else
tabHost.setCurrentTab(0);
}
}
浏览此链接http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/
你可以使用手势检测器
在nTouchEvent(MotionEvent)方法中,确保你调用了GestureDetector
使用提供的MotionEvents检测各种手势和事件。GestureDetector。当一个特定的动作事件发生时,OnGestureListener回调将通知用户。这个类只能用于通过touch报告的MotionEvents(不要用于trackball事件)。使用这个类:
2 onTouchEvent (MotionEvent)。在回调中定义的方法将在事件发生时执行
这个页面展示了如何使用GestureDetector来识别一个滑动手势:
http://smartandroidians.blogspot.in/2010/04/swipe-action-and-viewflipper-in-android.html我使用手势检测器…你的Fragment需要实现OnGestureListener。然后在onCreateView中初始化TabHost。
private FragmentTabHost mTabHost; // global in fragment
下面是代码:
private GestureDetectorCompat gDetector; // global in fragment
- 在onCreateView
gDetector = new GestureDetectorCompat(getActivity(), new OnGestureListener() { @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i("motion", "onFling has been called!"); final int SWIPE_MIN_DISTANCE = 120; final int SWIPE_MAX_OFF_PATH = 250; final int SWIPE_THRESHOLD_VELOCITY = 200; try { if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false; if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Log.i("motion", "Right to Left"); switchTabs(false); } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { Log.i("motion", "Left to Right"); switchTabs(true); } } catch (Exception e) { // nothing } return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } }); mTabHost.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return gDetector.onTouchEvent(event); } });
方法switchTabs(boolean Direction)
public void switchTabs(boolean direction) { Log.w("switch Tabs", "idemo direction"); if (direction) // true = move left { if (mTabHost.getCurrentTab() != 0) mTabHost.setCurrentTab(mTabHost.getCurrentTab() - 1); } else // move right { if (mTabHost.getCurrentTab() != (mTabHost.getTabWidget() .getTabCount() - 1)) mTabHost.setCurrentTab(mTabHost.getCurrentTab() + 1); } }
如果您已经创建了自定义活动和视图,那么我建议这样做:
实现 OnGestureListener
接口在你的当前活动和覆盖 onfling
方法。
实现完全取决于你。