首页 > 代码库 > Adding Navigation Tabs
Adding Navigation Tabs
实现ActionBar.TabListener接口
public static class TabListener<T extends Fragment> implements ActionBar.TabListener{ private Fragment mFragment; private final Activity mActivity; private final String mTag; private final Class<T> mClass; public TabListener(Activity activity, String tag, Class<T> clz) { mActivity = activity; mTag = tag; mClass = clz; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if (mFragment == null) { // If not, instantiate and add it to the activity mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, mFragment, mTag); } else { ft.attach(mFragment); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { if (mFragment != null) { ft.detach(mFragment); } } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 4 ActionBar actionBar = getActionBar(); 5 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 6 actionBar.setDisplayShowTitleEnabled(false); 7 8 Tab tab = actionBar.newTab() 9 .setText("tab1")10 .setTabListener(new TabListener<PlaceholderFragment>(11 this, "artist", PlaceholderFragment.class));12 actionBar.addTab(tab);13 14 tab = actionBar.newTab()15 .setText("tab2")16 .setTabListener(new TabListener<PlaceholderFragment>(17 this, "album", PlaceholderFragment.class));18 actionBar.addTab(tab);19 20 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。