滑動效果很是流暢,能夠將頁籤放置到ActionBar上,點擊頁籤能夠切換ViewPage,滑動ViewPage會同步更顯Tabs。java
main.xmlandroid
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 定義導航狀態條組件 --> <android.support.v4.view.PagerTitleStrip android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:background="#33b5e5" android:paddingBottom="4dp" android:paddingTop="4dp" android:textColor="#fff" /> </android.support.v4.view.ViewPager>
ActionBar_TabNavSwipe.javaweb
package org.crazyit.ui; import android.app.ActionBar; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; /** * Description: <br/> * website: <a href="http://www.crazyit.org">crazyit.org</a> <br/> * Copyright (C), 2001-2014, Yeeku.H.Lee <br/> * This program is protected by copyright laws. <br/> * Program Name: <br/> * Date: * * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class ActionBar_TabSwipeNav extends FragmentActivity implements ActionBar.TabListener { ViewPager viewPager; ActionBar actionBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 獲取ActionBar對象 actionBar = getActionBar(); // 獲取ViewPager viewPager = (ViewPager) findViewById(R.id.pager); // 建立一個FragmentPagerAdapter對象,該對象負責爲ViewPager提供多個Fragment FragmentPagerAdapter pagerAdapter = new FragmentPagerAdapter( getSupportFragmentManager()) { // 獲取第position位置的Fragment @Override public Fragment getItem(int position) { Fragment fragment = new DummyFragment(); Bundle args = new Bundle(); args.putInt(DummyFragment.ARG_SECTION_NUMBER, position + 1); fragment.setArguments(args); return fragment; } // 該方法的返回值i代表該Adapter總共包括多少個Fragment @Override public int getCount() { return 3; } // 該方法的返回值決定每一個Fragment的標題 @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "第一頁"; case 1: return "第二頁"; case 2: return "第三頁"; } return null; } }; // 設置ActionBar使用Tab導航方式 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // 遍歷pagerAdapter對象所包含的所有Fragment。 // 每一個Fragment對應建立一個Tab標籤 for (int i = 0; i < pagerAdapter.getCount(); i++) { actionBar .addTab(actionBar.newTab() .setText(pagerAdapter.getPageTitle(i)) .setTabListener(this)); } // 爲ViewPager組件設置FragmentPagerAdapter viewPager.setAdapter(pagerAdapter); // ① // 爲ViewPager組件綁定事件監聽器 viewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { // 當ViewPager顯示的Fragment發生改變時激發該方法 @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } // 當指定Tab被選中時激發該方法 @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { viewPager.setCurrentItem(tab.getPosition()); // ② } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } }
DummyFragment.javaapp
package org.crazyit.ui; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * Description: <br/> * 網站: <a href="http://www.crazyit.org">瘋狂Java聯盟</a> <br/> * Copyright (C), 2001-2014, Yeeku.H.Lee <br/> * This program is protected by copyright laws. <br/> * Program Name: <br/> * Date: * * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */ public class DummyFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; // 該方法的返回值就是該Fragment顯示的View組件 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER_HORIZONTAL); // 獲取建立該Fragment時傳入的參數Bundle Bundle args = getArguments(); // 設置TextView顯示的文本 textView.setText(args.getInt(ARG_SECTION_NUMBER) + ""); textView.setTextSize(30); // 返回該TextView return textView; } }
截圖:ide