我想在其中添加可交换的Acion栏标签

时间:2015-10-26 19:46:56

标签: android

我有操作栏标签工作。我想添加交换触摸选项。

我有操作栏标签工作。我想添加交换触摸选项。请参阅下面提供的代码:

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RelativeLayout;
public class ActionBarMain extends Activity implements TabListener {
RelativeLayout rl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_action_bar_main);
    try {
        rl = (RelativeLayout) findViewById(R.id.mainLayout);
        fragMentTra = getFragmentManager().beginTransaction();
        ActionBar bar = getActionBar();
        bar.addTab(bar.newTab().setText("Home").setTabListener(this));
        bar.addTab(bar.newTab().setText("Workouts").setTabListener(this));
        bar.addTab(bar.newTab().setText("Details").setTabListener(this));

        bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
                | ActionBar.DISPLAY_USE_LOGO & ActionBar.DISPLAY_SHOW_TITLE );
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowHomeEnabled(true);
        bar.setDisplayShowTitleEnabled(true);
        bar.show();

    } catch (Exception e) {
        e.getMessage();
    }
    /**
     * Hiding Action Bar
     */
}

FragMent1 fram1;
FragmentTransaction fragMentTra = null;
FragMent2 fram2;
FragMent3 fram3;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_action_bar_main, menu);
    return true;
}

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

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

    if (tab.getText().equals("Workouts")) {
        try {
            rl.removeAllViews();
        } catch (Exception e) {
        }
        fram1 = new FragMent1();
        fragMentTra.addToBackStack(null);
        fragMentTra = getFragmentManager().beginTransaction();
        fragMentTra.add(rl.getId(), fram1);
        fragMentTra.commit();
    } else if (tab.getText().equals("Home")) {
        try {
            rl.removeAllViews();
        } catch (Exception e) {
        }
        fram2 = new FragMent2();
        fragMentTra.addToBackStack(null);
        fragMentTra = getFragmentManager().beginTransaction();
        fragMentTra.add(rl.getId(), fram2);
        fragMentTra.commit();
    } else if (tab.getText().equals("Details")) {
        try {
            rl.removeAllViews();
        } catch (Exception e) {
        }
        fram3 = new FragMent3();
        fragMentTra.addToBackStack(null);
        fragMentTra = getFragmentManager().beginTransaction();
        fragMentTra.add(rl.getId(), fram3);
        fragMentTra.commit();
    }

}

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

}

}

1 个答案:

答案 0 :(得分:0)

您必须使用ViewPager以及FragmentPagerAdapter或FragmentStatePagerAdpater。这是开发者网站的链接。

http://developer.android.com/training/implementing-navigation/lateral.html