当我创建FragmentPagerAdapter并将页面数定义为3时,页面1将包含内容,页面2将不包含内容,页面3将包含内容。如果我将页数更改为4或更多,则每个页面都有内容。为什么会这样呢?如果我创建了2个页面,没有内容吗?我错过了什么?
下面是我的代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int position) {
/*
* Here we are going to return the correct type of fragment for each
* of the pages
*/
Log.d("Position", "Default Postion: " + position);
return MainListFragment.init(position);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}我意识到我所有的内容都是一样的,这没什么。这里的重点是,当我有3个页面时,只有2个页面有内容。如果我有4个或更多的页面,一切都会按预期进行。
更奇怪的是,我将输出放到了ListFragment的onCreate方法中,它实际上创建了所有的ListFragments,但并没有全部显示出来。
编辑:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_feed);
RequestUserFeed feed = new RequestUserFeed();
feed.execute(URL);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}下面是代码的其余部分:
public static class MainListFragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
int fragNum;
public static final String ARG_SECTION_NUMBER = "section_number";
private static MainListFragment init(int val) {
MainListFragment mfl = new MainListFragment();
Bundle args = new Bundle();
args.putInt("val", val);
mfl.setArguments(args);
return mfl;
}
public MainListFragment() {
}
/**
* Retrieving this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment", "Fragment Created for " + getArguments().getInt("val"));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.setting_fragment,
container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SettingListAdapter settingAdapter = new SettingListAdapter(
getActivity().getApplicationContext(),
R.layout.setting_item_row, MainFeedActivity.mProfileList);
ListView listView = (ListView) getActivity().findViewById(android.R.id.list);
listView.setAdapter(settingAdapter);
}
}在当前状态下,我只能在一个页面上获得列表视图。如果我移动这段代码:
SettingListAdapter settingAdapter = new SettingListAdapter(
getActivity().getApplicationContext(),
R.layout.setting_item_row, MainFeedActivity.mProfileList);
ListView listView = (ListView) getActivity().findViewById(android.R.id.list);
listView.setAdapter(settingAdapter);对于onCreateView,我将得到一个不同的结果。在onCreateView中设置列表适配器将填充更多页面。令人困惑的是,我知道,但我觉得它值得一提。
发布于 2013-10-06 13:20:36
为什么在getItem的switch中有这样定义的位置。你有3个项目,所以位置将是0,1,2:
switch (position) {
case 0:
Log.d("Position", "Postion: " + position);
return MainListFragment.init(position);
case 1:
Log.d("Position", "Postion: " + position);
return MainListFragment.init(position);
case 2:
Log.d("Position", "Postion: " + position);
return MainListFragment.init(position);
}https://stackoverflow.com/questions/19205679
复制相似问题