Android ViewPager w/ multiple Fragment classes each w/ separate layout file

主宰稳场 提交于 2019-12-18 06:56:20

问题


My app uses the android.support.v4.view.ViewPager class. For each page in the ViewPager, I want there to be one Fragment, each with its own layout file. Each Fragment is defined in its own file. I am having a hard time implementing this. To clarify, there is:

1 ViewPager
4 Fragment Files - FragmentOne.class, FragmentTwo.class, FragmentThree.class, FragmentFour.class
4 XML Layout Files - frag_one.xml, frag_two.xml, frag_three.xml, frag_four.xml

The examples that I have seen only deal with one layout file.

Here is my FragmentActivity where the ViewPager is held:

public class Main extends FragmentActivity {
    MyAdapter mAdapter;
    ViewPager mPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

    }

    public static class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Fragment getItem(int position) {
            return EditorFragment.newInstance(position);
        }
    }

}

My main question is: How do I get the adapter, MyAdapter, to recognize the four separate Fragment files and smoothly page across them with ViewPager? What kind of modifications should I make to getItem()?

Any help is appreciated.

EDIT:

So, I tried what lulumeya suggested and came up with this:

EDIT 2: Yes, those break statements fixed it. It works now. Thanks to everyone who helped! Now I have a nice segregation, thanks.

    @Override
    public Fragment getItem(int position) {
        Fragment f = new Fragment();

        switch (position) {
        case 0:
            f = FragmentOne.newInstance(position);
                    break;
        case 1:
            f = FragmentTwo.newInstance(position);
                    break;
        case 2:
            f = FragmentThree.newInstance(position);
                    break;
        case 3:
            f = FragmentFour.newInstance(position);
                    break;
        }

        return f;
    }

The app lauches, which is always a good sign, but is stuck on layout four. When I page to the right it is also layout four, for all pages. I was sure to change the layout in the inflater for each Fragment. I am still going to work on this. @lulumeya - Is this what you meant?


回答1:


Just return proper Fragment instance in getItem(int position) by position. use switch~case, It's easy.



来源:https://stackoverflow.com/questions/9146232/android-viewpager-w-multiple-fragment-classes-each-w-separate-layout-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!