[Android][App]Tabbed Activity 中的 key code: getItem()
阿新 • • 發佈:2018-12-25
今天的需求要用到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.