android:ViewPager显示Fragment(碎片)
private ViewPager viewPager; viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setAdapter(new SectionsPagerAdapter( getSupportFragmentManager()));
适配器
/** * viewpager适配器 * * @author Administrator * */ private class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { Fragment fragment = null; switch (arg0) { case 0: //碎片A fragment = new RecomFragment(); break; case 1: case 2: case 3: case 4: //碎片B fragment = new CommFragment(); break; case 5: //碎片C fragment = new OtherMoreFragment(); break; case 6: //碎片D fragment = new OtherChannelEditFragment(); break; } return fragment; } @Override public int getCount() { return 7; } }
其中所有碎片都要继承:android.support.v4.app.Fragment