小智 6
您可以覆盖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;
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19592 次 |
最近记录: |