码迷,mamicode.com
首页 > 移动开发 > 详细

Android ActionBar与ViewPager合用

时间:2014-05-12 16:08:41      阅读:409      评论:0      收藏:0      [点我收藏+]

标签:android   des   style   blog   class   code   

 

bubuko.com,布布扣

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

 

view_pager_main.xml

bubuko.com,布布扣
<?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>
bubuko.com,布布扣


activity

bubuko.com,布布扣
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);
        }
    }
}
bubuko.com,布布扣

Android ActionBar与ViewPager合用,布布扣,bubuko.com

Android ActionBar与ViewPager合用

标签:android   des   style   blog   class   code   

原文地址:http://www.cnblogs.com/wuyou/p/3704119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!