利用ViewPager+Fragment+actionbar實現可左右滑動的Action Tab
阿新 • • 發佈:2019-02-14
- 設定 ActionBar 的樣式,如:無回退按鍵、無標題等——setUpActionBar()
- 初始化並設定 ViewPager,兩個要點:首先要定義並設定 ViewPager 所需要的資料控制類 ViewPagerAdapter,ViewPagerAdapter 必須實現如何返回對應位置的 Fragment、頁數、頁標題;其次,要實現 ViewPager 的翻頁監聽介面(OnPageChangeListener),主要是實現當前頁面被切換時需要導航到對應位置的action tab button,必要的時候,還需要實現在頁面翻動的各個狀態下(正在翻動、翻動即將停止、翻動完全停止)的相應處理——setUpViewPager()
- 然後需要建立三個 tab button,並實現 tab 的監聽介面:主要是當選擇相應 tab 時,如何切換 ViewPager 的相應頁面——setUpTabs()
- ActionTabWithSlippingFragmentActivity.java 的實現部分
-
- publicclass ActionTabWithSlippingFragmentActivity extends FragmentActivity
- implements ActionBar.TabListener{
-
private Fragment1 mFragment1 =
- private Fragment2 mFragment2 = new Fragment2();
- private Fragment3 mFragment3 = new Fragment3();
- privatestaticfinalint TAB_INDEX_COUNT = 3;
- privatestaticfinalint TAB_INDEX_ONE = 0;
- privatestaticfinalint TAB_INDEX_TWO = 1;
-
privatestaticfinalint
- private ViewPager mViewPager;
- private ViewPagerAdapter mViewPagerAdapter;
- /** Called when the activity is first created. */
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- setUpActionBar();
- setUpViewPager();
- setUpTabs();
- }
- privatevoid setUpActionBar() {
- final ActionBar actionBar = getActionBar();
- actionBar.setHomeButtonEnabled(false);
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- actionBar.setDisplayShowTitleEnabled(false);
- actionBar.setDisplayShowHomeEnabled(false);
- }
- privatevoid setUpViewPager() {
- mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
- mViewPager = (ViewPager)findViewById(R.id.pager);
- mViewPager.setAdapter(mViewPagerAdapter);
- mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
- @Override
- publicvoid onPageSelected(int position) {
- final ActionBar actionBar = getActionBar();
- actionBar.setSelectedNavigationItem(position);
- }
- @Override
- publicvoid onPageScrollStateChanged(int state) {
- switch(state) {
- case ViewPager.SCROLL_STATE_IDLE:
- //TODO
- break;
- case ViewPager.SCROLL_STATE_DRAGGING:
- //TODO
- break;
- case ViewPager.SCROLL_STATE_SETTLING:
- //TODO
- break;
- default:
- //TODO
- break;
- }
- }
- });
- }
- privatevoid setUpTabs() {
- final ActionBar actionBar = getActionBar();
- for (int i = 0; i < mViewPagerAdapter.getCount(); ++i) {
- actionBar.addTab(actionBar.newTab()
- .setText(mViewPagerAdapter.getPageTitle(i))
- .setTabListener(this));
- }
- }
- @Override
- protectedvoid onDestroy() {
- super.onDestroy();
- }
- publicclass ViewPagerAdapter extends FragmentPagerAdapter {
- public ViewPagerAdapter(FragmentManager fm) {
- super(fm);
- // TODO Auto-generated constructor stub
- }
- @Override
- public Fragment getItem(int position) {
- // TODO Auto-generated method stub
- switch (position) {
- case TAB_INDEX_ONE:
- return mFragment1;
- case TAB_INDEX_TWO:
- return mFragment2;
- case TAB_INDEX_THREE:
- return mFragment3;
- }
- thrownew IllegalStateException("No fragment at position " + position);
- }
- @Override
- publicint getCount() {
- // TODO Auto-generated method stub
- return TAB_INDEX_COUNT;
- }
- @Override
- public CharSequence getPageTitle(int position) {
- String tabLabel = null;
- switch (position) {
- case TAB_INDEX_ONE:
- tabLabel = getString(R.string.tab_1);
- break;
- case TAB_INDEX_TWO:
- tabLabel = getString(R.string.tab_2);
- break;
- case TAB_INDEX_THREE: