1

我想在此活动中添加滑动以在其片段之间切换,现在应用程序正在检测运动。选项卡切换应该是通过 SwipeLeft 和 SwipeRight 函数进行的,但我不知道该怎么做。有人可以解释一下我该怎么做吗?

public class Cantina extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tabA = actionBar.newTab();
    tabA.setText("seg");
    tabA.setTabListener(new TabListener<MyFragmentA>(this, "seg", MyFragmentA.class));
    actionBar.addTab(tabA);

    ... 3 tabs

    Tab tabE = actionBar.newTab();
    tabE.setText("sex");
    tabE.setTabListener(new TabListener<MyFragmentE>(this, "sex", MyFragmentE.class));
    actionBar.addTab(tabE);

    if (savedInstanceState != null) {
        int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
        getActionBar().setSelectedNavigationItem(savedIndex);
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
}

public static class TabListener<T extends Fragment> 
    implements ActionBar.TabListener{

    private final Activity myActivity;
    private final String myTag;
    private final Class<T> myClass;

    public TabListener(Activity activity, String tag, Class<T> cls) {
        myActivity = activity;
        myTag = tag;
        myClass = cls;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        // Check if the fragment is already initialized
        if (myFragment == null) {
            // If not, instantiate and add it to the activity
            myFragment = Fragment.instantiate(myActivity, myClass.getName());
            ft.add(android.R.id.content, myFragment, myTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(myFragment);
        }

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

        Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

        if (myFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(myFragment);
        }

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
    }

}
public void SwipeRight(){
    Toast.makeText(this, "RIGHT", Toast.LENGTH_LONG).show();    
}

private void SwipeLeft(){
    Toast.makeText(this, "LEFT", Toast.LENGTH_LONG).show();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener 
= new SimpleOnGestureListener(){

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        float sensitvity = 50;
        if((e1.getX() - e2.getX()) > sensitvity){
            SwipeLeft();
        }else if((e2.getX() - e1.getX()) > sensitvity){
            SwipeRight();
        }

        return true;
    }

};

GestureDetector gestureDetector
= new GestureDetector(simpleOnGestureListener);

}

4

1 回答 1

1

如果它符合你的要求,那么你就可以去做。来自 Android 开发者的官方指南。

使用标签滑动视图

于 2014-04-10T15:43:17.717 回答