我从github获得了以下代码,但我需要知道如何在调用getItem()方法时添加自定义xml布局。有人可以帮忙吗? 现在,应用程序仅显示CONTENT字符串,每次刷选或选择选项卡时,我都需要添加不同的自定义布局。
public class SampleTabsWithIcons extends FragmentActivity {
private static final String[] CONTENT = new String[] { "Calendar", "Camera","Alarms", "Location" };
private static final int[] ICONS = new int[] {
R.drawable.perm_group_calendar,
R.drawable.perm_group_camera,
R.drawable.perm_group_device_alarms,
R.drawable.perm_group_location,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_tabs);
FragmentPagerAdapter adapter = new GoogleMusicAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
class GoogleMusicAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
public GoogleMusicAdapter(FragmentManager fm) {
super(fm);
}
//This is the part i need help
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
@Override public int getIconResId(int index) {
return ICONS[index];
}
@Override
public int getCount() {
return CONTENT.length;
}
}
}
答案 0 :(得分:3)
只需要为每个布局创建不同的片段(如果它们不同)。创建每个Fragment的实例并返回getItem()方法。
@Override
public Fragment getItem(int position) {
if(position==0)
return AFragment.newInstance();
else if(position==1)
return BFragment.newInstance();
else if(position==2)
return CFragment.newInstance();
}