码迷,mamicode.com
首页 > 其他好文 > 详细

Adding Navigation Tabs

时间:2014-07-31 02:29:25      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   io   art   cti   ar   

实现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     }

 

Adding Navigation Tabs,布布扣,bubuko.com

Adding Navigation Tabs

标签:android   style   blog   color   io   art   cti   ar   

原文地址:http://www.cnblogs.com/lazyzhong/p/3879682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!