ViewPager,RadioGroup,FragmentManager 详解 <五> Fragment,ViewPager 和ActionBar

这个 ActionBar 是导航部分的内容,它可以设置导航的模式为tab的模式,可以添加tab到actionbar中

步骤如下:

0:正常的ViewPager 和Fragment的显示,

1:设置ActionBar

2:实现ActionBar和ViewPager的联动。


具体:

0:创建Fragment

1:创建布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v4.view.ViewPager  android:layout_width="match_parent"  android:layout_height="match_parent"  android:id="@+id/viewPager"></android.support.v4.view.ViewPager>
</RelativeLayout>

2:创建adatper,但是这里不用getPagerTitle了

package tech.androidstudio.actionbarviewpagerfragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**  * Created by Kodulf on 2016/3/14.  */ public class ActionBarFragmentPagerAdapter extends FragmentPagerAdapter {
    List<Fragment> list;

    public ActionBarFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

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

4:在MainActivity 里面实现联动,其中报错获取actionBar,获取Tab,等

package tech.androidstudio.actionbarviewpagerfragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

    private ActionBar mActionBar;
    private ViewPager mViewPager;
    private ActionBar.Tab mTab0;
    private ActionBar.Tab mTab1;
    private ActionBar.Tab mTab2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager = (ViewPager)findViewById(R.id.viewPager);
        List<Fragment> mList = new ArrayList<Fragment>();
        mList.add(new FristFragment());
        mList.add(new SecondFragment());
        mList.add(new ThirdFragment());
        ActionBarFragmentPagerAdapter adapter = new ActionBarFragmentPagerAdapter(getSupportFragmentManager(),mList);

        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(this);

        //获取支持保重的 actionBar mActionBar = getSupportActionBar();
        if(mActionBar !=null){
            //设置actionBar的导航模式为ActionBar.NAVIGATION_MODE_TABS  //然后通过ActionBar.newTab()方法来获取 新的 Tab,然后设置tab的 文字和监听器。最后添加到ActionBar里去  mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            mTab0 = mActionBar.newTab();
            mTab0.setText("财经");
            mTab0.setTabListener(this);
            mActionBar.addTab(mTab0);

            mTab1 = mActionBar.newTab();
            mTab1.setText("体育");
            mTab1.setTabListener(this);
            mActionBar.addTab(mTab1);

            mTab2 = mActionBar.newTab();
            mTab2.setText("娱乐");
            mTab2.setTabListener(this);
            mActionBar.addTab(mTab2);

        }
    }

    // Tab的 回调方法 里面设置viewPager显示的页面  @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            switch (tab.getPosition()){
                case 0:
                    mViewPager.setCurrentItem(0);
                    break;
                case 1:
                    mViewPager.setCurrentItem(1);
                    break;
                case 2:
                    mViewPager.setCurrentItem(2);
                    break;
            }
    }

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

    }

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

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    //ViewPager的 回调方法里面设置 actionbar应该显示的tab  @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                mActionBar.selectTab(mTab0);
                break;
            case 1:
                mActionBar.selectTab(mTab1);
                break;
            case 2:
                mActionBar.selectTab(mTab2);
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}


你可能感兴趣的:(ViewPager,RadioGroup,FragmentManager 详解 <五> Fragment,ViewPager 和ActionBar)