• actionbarsherlock示例


    package com.example.viewpagerandtabdemo;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    
    import com.actionbarsherlock.app.ActionBar;
    import com.actionbarsherlock.app.ActionBar.Tab;
    import com.actionbarsherlock.app.SherlockFragmentActivity;
    
    public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener, OnPageChangeListener{
        /**
         * 顶部Tab的title
         */
        private String [] mTabTitles;
        
        /**
         * ViewPager对象的引用
         */
        private ViewPager mViewPager;
        
        /**
         * 装载Fragment的容器,我们的每一个界面都是一个Fragment
         */
        private List<Fragment> mFragmentList;
        
        /**
         * ActionBar对象的引用
         */
        private ActionBar mActionBar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            
            
            mViewPager = (ViewPager) findViewById(R.id.viewPager);
            mFragmentList =  new ArrayList<Fragment>();
            //设置Adapter
            mViewPager.setAdapter(new TabPagerAdapter(getSupportFragmentManager(), mFragmentList));
            //设置监听
            mViewPager.setOnPageChangeListener(this);
            
            
            
            //从资源文件在获取Tab的title
            mTabTitles = getResources().getStringArray(R.array.tab_title);
            //获取Action实例我们使用getSupportActionBar()方法
            mActionBar = getSupportActionBar();
            //隐藏Title
            mActionBar.setDisplayShowTitleEnabled(false);
            //隐藏Home logo
            mActionBar.setDisplayShowHomeEnabled(false);
            //设置ActionBar的导航模式为Tab
            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            
            
            //为ActionBar添加Tab并设置TabListener
            for(int i=0; i<mTabTitles.length; i++){
                 ActionBar.Tab tab = mActionBar.newTab();
                 tab.setText(mTabTitles[i]);
                 tab.setTabListener(this);
                 mActionBar.addTab(tab, i);
            }
            
            
            //将Fragment加入到List中,并将Tab的title传递给Fragment
            for(int i=0; i<mTabTitles.length; i++){
                Fragment fragment = new ItemFragment();
                Bundle args = new Bundle();
                args.putString("arg", mTabTitles[i]);
                fragment.setArguments(args);
                mFragmentList.add(fragment);
            }
            
        }
        
        
    
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            //点击ActionBar Tab的时候切换不同的Fragment界面
            mViewPager.setCurrentItem(tab.getPosition());
        }
    
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    
        }
    
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            
        }
        
        
        @Override
        public void onPageScrollStateChanged(int arg0) {
            
        }
    
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            
        }
    
        @Override
        public void onPageSelected(int arg0) {
            //滑动ViewPager的时候设置相对应的ActionBar Tab被选中
            mActionBar.setSelectedNavigationItem(arg0);
        }
    
    
    }
    package com.example.viewpagerandtabdemo;
    
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import com.actionbarsherlock.app.SherlockFragment;
    
    
    public class ItemFragment extends SherlockFragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            
            View contextView = inflater.inflate(R.layout.fragment_item, container, false);
            TextView mTextView = (TextView) contextView.findViewById(R.id.textview);
            
            //获取Activity传递过来的参数
            Bundle mBundle = getArguments();
            String title = mBundle.getString("arg");
            
            mTextView.setText(title);
            
            return contextView;
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    
    }
    package com.example.viewpagerandtabdemo;
    
    import java.util.List;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;
    
    public class TabPagerAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> list;
        
        //构造函数
        public TabPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }
    
        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }
    
        @Override
        public int getCount() {
            return list.size();
        }
    
    }

     插件用的是github上的actionbarsherlock插件。

  • 相关阅读:
    学习之路-前端-笔记-一、HTML笔记
    抄写例题作业1
    郭霖
    java遍历HashMap的高效方法
    Add Two Numbers II 两个数字相加之二
    基于socket的简单p2p聊天项目
    SimpleScalar Course Project
    Dijkstra Java
    PCA vs Linear Regression 可视化理解
    机器学习中的范数规则化之(一)L0、L1与L2范数
  • 原文地址:https://www.cnblogs.com/yaowen/p/5438622.html
Copyright © 2020-2023  润新知