ViewPager与ActionBar

2016-11-23  本文已影响0人  果冻都烂了

之前使用过 viewpager indicator开源框架

1.在官方借鉴一些样式信息

android:theme="@style/CustomActionBarTheme"

//actionbar的样式

              parent="@style/Theme.AppCompat.Light"

         parent="">

       @style/MyActionBarTabs

       @style/MyActionBarTabs

         parent="@style/Widget.AppCompat.ActionBar.TabView">

       @drawable/actionbar_tab_indicator

       @drawable/actionbar_tab_indicator

//top的选择器

         android:state_pressed="false"

         android:drawable="@drawable/tab_unselected_example" />

         android:state_pressed="false"

         android:drawable="@drawable/tab_selected_example" />

         android:state_pressed="false"

         android:drawable="@drawable/tab_unselected_focused_example" />

         android:state_pressed="false"

         android:drawable="@drawable/tab_selected_focused_example" />

         android:state_pressed="true"

         android:drawable="@drawable/tab_unselected_pressed_example" />

         android:state_pressed="true"

         android:drawable="@drawable/tab_selected_pressed_example" />

         android:state_pressed="true"

         android:drawable="@drawable/tab_unselected_pressed_example" />

         android:state_pressed="true"

         android:drawable="@drawable/tab_selected_pressed_example" />

2.获取actionbar对象 设置属性

ActionBar actionBar = getSupportActionBar();

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//添加头部信息

ActionBar.Tab tab = actionBar.newTab()

       .setText("home")

       .setTabListener(new MyTabListener(

               this, "home", HomeFragment.class));

actionBar.addTab(tab);

//创建toblistener类

public class MyTabListener implements ActionBar.TabListener{

   private Fragment mFragment;

   private final Activity mActivity;

   private final String mTag;

   private final Class mClass;

   /** Constructor used each time a new tab is created.

   * @param activity  The host Activity, used to instantiate the fragment

   * @param tag  The identifier tag for the fragment

   * @param clz  The fragment's Class, used to instantiate the fragment

   */

   public MyTabListener(Activity activity, String tag, Class clz) {

       mActivity = activity;

       mTag = tag;

       mClass = clz;

   }

   @Override

   public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

     /* // Check if the fragment is already initialized

       if (mFragment == null) {

           // If not, instantiate and add it to the activity

           mFragment = Fragment.instantiate(mActivity, mClass.getName());

           ft.add(android.R.id.content, mFragment, mTag);

       } else {

           // If it exists, simply attach it in order to show it

           ft.attach(mFragment);

       }*/

        //viewpager与actionbar关联

       mViewpager.setCurrentItem(tab.getPosition());

   }

   @Override

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

   }

}

3.创建fragment   与viewpager

mViewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));

mViewpager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

   @Override

   public void onPageSelected(int position) {

       super.onPageSelected(position);

       //actionbar与viewpager关联

       getSupportActionBar().setSelectedNavigationItem(position);

   }

});

//设置adapter 继承 FragmentPagerAdapter

public class MyAdapter extends FragmentPagerAdapter{

   public MyAdapter(FragmentManager fm) {

       super(fm);

   }

   @Override

   public Fragment getItem(int position) {

       //创建你要添加的fragment

       Fragment fragment;

       if (position==0){

           fragment=new HomeFragment();

       }else {

           fragment=new AppFragment();

       }

       return fragment;

   }

   @Override

   public int getCount() {

       return 2;

   }

}

//在布局中添加上这段

   android:id="@+id/pager_title_strip"

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:layout_gravity="top"

   android:background="#33b5e5"

   android:textColor="#fff"

   android:paddingTop="4dp"

   android:paddingBottom="4dp" />

//在adapter中添加这个方法

@Override

public CharSequence getPageTitle(int position) {

   return str[position];

}

ViewPager与ActionBar
上一篇下一篇

猜你喜欢

热点阅读