使用标签主机滑动

nro*_*fis 2 android swipe android-tabhost nui

我有一个TabHost控件(不在操作栏中),我想在用户在每个选项卡上滑动上下文时更改选项卡(类似于whatsapp表情符号选项卡).
我怎样才能做到这一点?

编辑
感觉也很重要.我希望上下文应该有滚动动画(无论用户是否滑动或是否单击了选项卡).

小智 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)