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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/champwang/article/details/81356584

今天的需求要用到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.

猜你喜欢

转载自blog.csdn.net/champwang/article/details/81356584