ViewPager API

Layout 管理器允许用户可以在页面上,左右滑动来翻动页面。这个时候,你可以考虑实现PagerAdapter接口。

需要注意的是,这个类在不断更新中,所以在以后的版本中,API会有所改变。

ViewPager很多时候会结合Fragment一块使用,这种方法使得管理每个页面的生命周期变得很方便。其中,有一些adapter的具体实现,可以适合于这种ViewPager结合Fagment使用的情况。这些adapter包括: FragmentPagerAdapter,FragmentStatePagerAdapter, FragmentPagerAdapter,  FragmentStatePagerAdapter

下面的代码实例,演示了ViewPager如何结合ActionTabs来使用:

publicclassActionBarTabsPagerextendsActivity{

    ViewPager mViewPager;

    TabsAdapter mTabsAdapter;

 

    @Override

    protectedvoid onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

 

        mViewPager =newViewPager(this);

        mViewPager.setId(R.id.pager);

        setContentView(mViewPager);

 

        finalActionBar bar = getActionBar();

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        bar.setDisplayOptions(0,ActionBar.DISPLAY_SHOW_TITLE);

 

        mTabsAdapter =newTabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(bar.newTab().setText("Simple"),

                CountingFragment.class,null);

        mTabsAdapter.addTab(bar.newTab().setText("List"),

                FragmentPagerSupport.ArrayListFragment.class,null);

        mTabsAdapter.addTab(bar.newTab().setText("Cursor"),

                CursorFragment.class,null);

 

        if(savedInstanceState !=null){

            bar.setSelectedNavigationItem(savedInstanceState.getInt("tab",0));

        }

    }

 

    @Override

    protectedvoid onSaveInstanceState(Bundle outState){

        super.onSaveInstanceState(outState);

        outState.putInt("tab", getActionBar().getSelectedNavigationIndex());

    }

 

    /**

     * This is a helperclass that implements the management of tabs and all

     * details ofconnecting a ViewPager with associated TabHost. It relies on a

     * trick.  Normally a tab host has a simple API forsupplying a View or

     * Intent that eachtab will show.  This is not sufficientfor switching

     * betweenpages.  So instead we make the contentpart of the tab host

     * 0dp high (it isnot shown) and the TabsAdapter supplies its own dummy

     * view to show asthe tab content.  It listens to changesin tabs, and takes

     * care of switchto the correct paged in the ViewPager whenever the selected

     * tab changes.

     */

    publicstaticclassTabsAdapterextendsFragmentPagerAdapter

            implementsActionBar.TabListener,ViewPager.OnPageChangeListener{

        privatefinalContext mContext;

        privatefinalActionBar mActionBar;

        privatefinalViewPager mViewPager;

        privatefinalArrayList<TabInfo> mTabs =newArrayList<TabInfo>();

 

        staticfinalclassTabInfo{

            privatefinalClass<?> clss;

            privatefinalBundle args;

 

            TabInfo(Class<?> _class,Bundle _args){

                clss = _class;

                args = _args;

            }

        }

 

        publicTabsAdapter(Activity activity,ViewPager pager){

            super(activity.getFragmentManager());

            mContext = activity;

            mActionBar = activity.getActionBar();

            mViewPager = pager;

            mViewPager.setAdapter(this);

            mViewPager.setOnPageChangeListener(this);

        }

 

        publicvoid addTab(ActionBar.Tab tab,Class<?> clss,Bundle args){

            TabInfo info =newTabInfo(clss, args);

            tab.setTag(info);

            tab.setTabListener(this);

            mTabs.add(info);

            mActionBar.addTab(tab);

           notifyDataSetChanged();

        }

 

        @Override

        publicint getCount(){

            return mTabs.size();

        }

 

        @Override

        publicFragment getItem(int position){

            TabInfo info = mTabs.get(position);

            returnFragment.instantiate(mContext, info.clss.getName(), info.args);

        }

 

        @Override

        publicvoid onPageScrolled(int position,float positionOffset,int positionOffsetPixels){

        }

 

        @Override

        publicvoid onPageSelected(int position){

            mActionBar.setSelectedNavigationItem(position);

        }

 

        @Override

        publicvoid onPageScrollStateChanged(int state){

        }

 

        @Override

        publicvoid onTabSelected(Tab tab,FragmentTransaction ft){

            Object tag = tab.getTag();

            for(int i=0; i<mTabs.size(); i++){

                if(mTabs.get(i)== tag){

                    mViewPager.setCurrentItem(i);

                }

            }

        }

 

        @Override

        publicvoid onTabUnselected(Tab tab,FragmentTransaction ft){

        }

 

        @Override

        publicvoid onTabReselected(Tab tab,FragmentTransaction ft){

        }

    }

}

 

你可能感兴趣的:(api,object,layout,null,Class,tabs)