简体   繁体   English

android-viewpager在每个选项卡中有多个子片段

[英]android-viewpager with multiple sub-fragments in each tab

This is giving me headache. 这让我头疼。 Lets say my app has a ViewPager which contains 4 tabs. 可以说我的应用程序有一个ViewPager ,其中包含4个标签。 I want to have multiple sub-fragments within each tab. 我希望每个选项卡中都有多个子片段。 that means each tab would have more than just one view. 这意味着每个选项卡将具有多个视图。 I totally have no clue how to achieve this.... 我完全不知道如何实现这一目标。

I have a FragmentActivity which gets load first and set my ViewPager 我有一个FragmentActivity ,它首先加载并设置我的ViewPager

public class FragActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frag_main);

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter adapter = new FFNPagerAdapter(getSupportFragmentManager());

        pager.setAdapter(adapter);
        pager.setOffscreenPageLimit(3);
        tabs.setViewPager(pager);
    }
}

I then have my FragmentPagerAdapter 然后我有我的FragmentPagerAdapter

public class PagerAdapter extends FragmentPagerAdapter implements TextAndIconTabProvider {
    private final String[] TITLES = { "FIRST", "SECOND", "THIRD", "FORTH" };

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment frag = new Fragment();
        switch (position) {
        case 1:
            frag = new FirstFrag();
            break;
        case 2:
            frag = new SecondFrag();
            break;
        case 3:
            frag = new ThirdFrag();
            break;
        case 4:
            frag = new ForthFrag();
            break;

        }

        return frag;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return TITLES.length;
    }
}

the above would give me a working ViewPager with 4 active tabs. 上面的代码会给我一个带有4个活动标签的ViewPager Then I need to do all 4 individual Fragments. 然后,我需要做所有4个单独的片段。 I'm giving out one example below. 我在下面给出一个例子。

public class FirstFrag extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.firstfrag, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Button btn1 = (Button) getActivity().findViewById(R.id.btn1);
        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // what should I put in this block??????   
                Fragment fragment = new FirstFragSub1();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.pager, fragment); //<<------what should i replace???????
                transaction.commitAllowingStateLoss();
            }
        });
    }
}

The OnCreateView in FirstFragSub1 gets called but I don't see anything displayed on the screen. FirstFragSub1OnCreateView被调用,但屏幕上没有显示任何内容。 Basically just a blank. 基本上只是一片空白。 I know by replacing R.id.pager should not be the correct way, I'm just giving out an example of what I initial thought was. 我知道通过替换R.id.pager应该不是正确的方法,我只是在举例说明我最初的想法。

Here is my FirstFragSub1 这是我的FirstFragSub1

public class FirstFragSub1 extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.i("FirstFragSub1","onCreateView is called");
        return inflater.inflate(R.layout.subone, container, false);
    }
}

Can someone please help me? 有人可以帮帮我吗? I really out of idea...Thanks 我真的不知道...谢谢

there is a great tutorial and github here that covers this in detail. 这里有一个很棒的教程和github,详细介绍了这一点。

http://tausiq.wordpress.com/2014/06/06/android-multiple-fragments-stack-in-each-viewpager-tab/ https://github.com/tausiq/ViewPagerMultipleFragmentDemo http://tausiq.wordpress.com/2014/06/06/android-multiple-fragments-stack-in-each-viewpager-tab/ https://github.com/tausiq/ViewPagerMultipleFragmentDemo

this covers handling the backstack and multiple tabs with multiple views per tab. 这涵盖了处理后退堆栈和每个选项卡具有多个视图的多个选项卡。 hope this help I was searching for a similar solution when i read your question. 希望当我阅读您的问题时,我正在寻找类似的解决方案。

Have FrameLayouts in the view that is being inflated by the Fragment. 在由Fragment放大的视图中具有FrameLayouts。 And onCreateView, replace the FrameLayout by the sub fragments that you want. 然后在onCreateView上,将FrameLayout替换为所需的子片段。

Also, be careful not to use the Activity's FragmentManager. 另外,请注意不要使用活动的FragmentManager。 getFragmentManager returns the Activity's FragmentManager and that will lead to an undesirable result (Only one fragment inside your ViewPager would have the sub fragments loaded). getFragmentManager返回活动的FragmentManager,这将导致不良结果(仅ViewPager中的一个片段会加载子片段)。 Use the getChildFragmentManager function instead. 请改用getChildFragmentManager函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM