首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FragmentPagerAdapter不工作

FragmentPagerAdapter不工作
EN

Stack Overflow用户
提问于 2013-10-06 13:16:55
回答 1查看 459关注 0票数 0

当我创建FragmentPagerAdapter并将页面数定义为3时,页面1将包含内容,页面2将不包含内容,页面3将包含内容。如果我将页数更改为4或更多,则每个页面都有内容。为什么会这样呢?如果我创建了2个页面,没有内容吗?我错过了什么?

下面是我的代码:

代码语言:javascript
复制
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个或更多的页面,一切都会按预期进行。

更奇怪的是,我将输出放到了ListFragmentonCreate方法中,它实际上创建了所有的ListFragments,但并没有全部显示出来。

编辑:

代码语言:javascript
复制
    @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);
    }

下面是代码的其余部分:

代码语言:javascript
复制
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);
            }


    }

在当前状态下,我只能在一个页面上获得列表视图。如果我移动这段代码:

代码语言:javascript
复制
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中设置列表适配器将填充更多页面。令人困惑的是,我知道,但我觉得它值得一提。

EN

回答 1

Stack Overflow用户

发布于 2013-10-06 13:20:36

为什么在getItem的switch中有这样定义的位置。你有3个项目,所以位置将是0,1,2:

代码语言:javascript
复制
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); 
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19205679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档