之前使用過(guò) viewpager indicator開(kāi)源框架
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對(duì)象 設(shè)置屬性
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);
//創(chuàng)建toblistener類(lèi)
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關(guān)聯(lián)
? ? ? ?mViewpager.setCurrentItem(tab.getPosition());
? ?}
? ?@Override
? ?public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
? ?}
}
3.創(chuàng)建fragment ? 與viewpager
mViewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));
mViewpager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
? ?@Override
? ?public void onPageSelected(int position) {
? ? ? ?super.onPageSelected(position);
? ? ? ?//actionbar與viewpager關(guān)聯(lián)
? ? ? ?getSupportActionBar().setSelectedNavigationItem(position);
? ?}
});
//設(shè)置adapter 繼承 FragmentPagerAdapter
public class MyAdapter extends FragmentPagerAdapter{
? ?public MyAdapter(FragmentManager fm) {
? ? ? ?super(fm);
? ?}
? ?@Override
? ?public Fragment getItem(int position) {
? ? ? ?//創(chuàng)建你要添加的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中添加這個(gè)方法
@Override
public CharSequence getPageTitle(int position) {
? ?return str[position];
}
