1. 程式人生 > >[Android][App]Tabbed Activity 中的 key code: getItem()

[Android][App]Tabbed Activity 中的 key code: getItem()

今天的需求要用到Tabbed  Activity, 此元件 android 自帶。

用於選擇不同的 Tab 頁顯示不同的內容,在 getItem()中實現。

主要程式碼如下:

 /**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {
	public SectionsPagerAdapter(FragmentManager fm) {
		super(fm);
	}

	public Fragment getItem(int position) {
		switch (position) {
			case 0: return new Fragment1();
			case 1: return new Fragment2();
			case 2: return new Fragment3();
			default: break;
		}
		return null;
	}

	public int getCount() {
		// Show 3 total pages.
		return 3;
	}
}

注意,getItem中返回的 Fragment.

end.