首页 > 代码库 > Android ActionBar与ViewPager合用

Android ActionBar与ViewPager合用

 

内容可以左右滑动,因为使用了ViewPager(我这里用了V4包)

 

view_pager_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/viewPager"
           android:layout_width="match_parent"
           android:layout_height="match_parent">

 </android.support.v4.view.ViewPager>


activity

package com.example.actionBarTest.actionBarTabViewPager;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import com.example.actionBarTest.R;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Heyiyong on 2014-5-2 上午10:17.
 */
public class ViewPagerTabActivity extends FragmentActivity implements ActionBar.TabListener {
    private ViewPager viewPager;
    private List<Fragment> fragments;
    private ActionBar actionBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_pager_main);

        HomeFragment homeFragment = new HomeFragment();
        MusicFragment musicFragment = new MusicFragment();
        VideoFragment videoFragment = new VideoFragment();
        fragments = new ArrayList<Fragment>();
        fragments.add(homeFragment);
        fragments.add(musicFragment);
        fragments.add(videoFragment);

        //设置ViewPager部分
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        //设置actionBar部分
        actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.addTab(actionBar.newTab().setText("主页").setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("音乐").setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("视频").setTabListener(this));
        }

        //viewPager与actionB整合部分
        //当viewPager翻页的时候,actionBar的tab也要随之改变
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
            }

            /**
             * 当页面选择了之后调用该方法
             */
            @Override
            public void onPageSelected(int i) {
                actionBar.setSelectedNavigationItem(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }


    //————————————————————————————————————————-
    //—————————————actionBar的回调 ———————————————————-
    //————————————————————————————————————————-
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }

    /**
     * viewPager的适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * 只会运行一次
         */
        @Override
        public Fragment getItem(int i) {
            System.out.println("com.example.actionBarTest.actionBarTabViewPager.ViewPagerTabActivity.MyPagerAdapter.getItem");
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            System.out.println("com.example.actionBarTest.actionBarTabViewPager.ViewPagerTabActivity.MyPagerAdapter.instantiateItem");
            return super.instantiateItem(container, position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            System.out.println("com.example.actionBarTest.actionBarTabViewPager.ViewPagerTabActivity.MyPagerAdapter.destroyItem");
            super.destroyItem(container, position, object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
            System.out.println("com.example.actionBarTest.actionBarTabViewPager.ViewPagerTabActivity.MyPagerAdapter.restoreState");
            super.restoreState(state, loader);
        }

        @Override
        public Parcelable saveState() {
            System.out.println("com.example.actionBarTest.actionBarTabViewPager.ViewPagerTabActivity.MyPagerAdapter.saveState");
            return super.saveState();
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            System.out.println("com.example.actionBarTest.actionBarTabViewPager.ViewPagerTabActivity.MyPagerAdapter.finishUpdate");
            super.finishUpdate(container);
        }
    }
}