Jerikc: 在Android4.0中,是經過ActionBar + tab + view Pager的方式實現的,而不是經過傳統的TabHost+tab的方式
仿照Android4.0撥號界面實現分頁的界面,以下: html
一、ViewPager是安卓的擴展包,能夠從源代碼out/target/common/obj/JAVA_LIBRARIES /android-support-v13-intermediates/classes.jar拷貝,注意是android-support-V13而 不是android-support-v4,由於Activity中getFragmentManager()返回的 android.app.FragmentManager對象,而v4中FragmentAdapter的構造函數參數類型 android.v4.Fragmentmanager,因此你懂的,不兼容,而v13完美兼容。 java
Jerikc:
jar包所在位置是\android-sdk\extras\android\support\v13\android-support-v13.jar
固然在v13目錄下,有一個Support13Demos,你們能夠用來學習參考!
步驟以下:
1. launch eclipse --> File --> New --> Project --> Android Project --> Create project from exist source -->
Browse... --> android-sdk_r15-linux--> extras --> android --> support --> v13 --> samples --> Supported13Demos --> OK
2. 選中項目Support13Demos --> 右擊 --> Properties --> Java build path --> Libraries --> Add external JARs.. --> android-support-v13.jar --> OK
二、main.xml佈局以下: linux
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/acti_frame"> <!-- 此處須要給出全路徑--> <android.support.v4.view.ViewPager android:id="@+id/viewpagerLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
三、自定義3個Fragment,Fragment在3.0推出,做用不少,使用方法推薦一個帖子 android
【eoeandroid索引】android fragment知識彙總
http://www.eoeandroid.com/thread-172624-1-1.html
Fragment的實現以下: app
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View root = inflater.inflate(R.layout.fragmentlayout1, container, false); return root; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="fragmentlayout1" android:textSize="22sp" android:gravity="center" /> </LinearLayout>
四、Activity中使用ViewPager和Fragment,代碼以下: eclipse
public class FragmentViewPagerTestActivity extends Activity { private final static String TAG = "FragmentViewPagerTestActivity"; private final PageChangeListener mPageChangeListener = new PageChangeListener(); private ViewPager vp = null; private final int TAB_INDEX1 = 0; private final int TAB_INDEX2 = 1; private final int TAB_INDEX3 = 2; private final int TAB_INDEX_COUNT = 3; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); initView(); vp.setOnPageChangeListener(mPageChangeListener); setupFragment1Tab(); setupFragment2Tab(); setupFragment3Tab(); getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); getActionBar().setDisplayShowTitleEnabled(false); getActionBar().setDisplayShowHomeEnabled(false); } private class PageChangeListener implements OnPageChangeListener { private int mCurrentPosition = -1; private int mNextPosition = -1; @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { final ActionBar actionBar = getActionBar(); if(mCurrentPosition == arg0) { Log.e(TAG , "Previous posion and next position is same"); } actionBar.selectTab(actionBar.getTabAt(arg0)); mNextPosition = arg0; } public void setCurrentPosition(int position) { mCurrentPosition = position; } } private TabListener mTabListener = new TabListener() { @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if(vp.getCurrentItem() != tab.getPosition()) { vp.setCurrentItem(tab.getPosition(),true); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { } }; private void setupFragment1Tab() { Tab tab = getActionBar().newTab(); tab.setText("Fragment1"); tab.setTabListener(mTabListener); getActionBar().addTab(tab); } private void setupFragment2Tab() { Tab tab = getActionBar().newTab(); tab.setText("Fragment2"); tab.setTabListener(mTabListener); getActionBar().addTab(tab); } private void setupFragment3Tab() { Tab tab = getActionBar().newTab(); tab.setText("Fragment3"); tab.setTabListener(mTabListener); getActionBar().addTab(tab); } private void findView() { vp = (ViewPager) findViewById(R.id.viewpagerLayout); } private void initView() { android.app.FragmentManager fm = getFragmentManager(); vp.setAdapter(new MyViewPagerAdapter(fm)); } public class MyViewPagerAdapter extends FragmentPagerAdapter { public MyViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { switch(arg0) { case TAB_INDEX1: return new Fragment1(); case TAB_INDEX2: return new Fragment2(); case TAB_INDEX3: return new Fragment3(); } return null; } @Override public int getCount() { return TAB_INDEX_COUNT; } } }PS:getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);指定定位模式爲TABs,不能少;