ViewPager-在片段页面中创建一个新片段 [英] ViewPager - In the fragment pages create a new fragment

查看:84
本文介绍了ViewPager-在片段页面中创建一个新片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager活动. ViewPager还有一个TabLayout,其中包含3个选项卡:FragmentAFragmentBFragmentC,每个选项卡对应于ViewPager中显示的3个页面之一.在FragmentB里面,尽管我会有一个按钮.现在,我需要能够单击此按钮并创建一个新的片段FragmentD,它将超过FragmentB.这个新片段需要有一个带有后退箭头的操作栏,以便可以返回到FragmentB.在所有这些过程中,选项卡仍应显示FragmentB是活动的选项卡.我已经研究了嵌套的片段,但是无法使其正常工作.我也尝试过从片段创建对viewpager活动的引用,并使用新片段的位置调用getItem方法,但这不起作用.这是我的代码.这是一个活动,它创建ViewPager.在ViewPager的内部创建了3个片段,同时还创建了TabLayout:

I have a ViewPager activity. The ViewPager also has a TabLayout with 3 tabs: FragmentA, FragmentB, and FragmentC, each corresponding to one of the 3 pages to be shown in the ViewPager. Inside of FragmentB though I will have a button. I now need to be able to click this button and have a new fragment created, FragmentD, which will overtake FragmentB. This new fragment needs to have an action bar with the back arrow so it can go back to FragmentB. The tabs should still show that FragmentB is the active tab during all of this. I have looked into nested fragments, but I could not make it work. I have also tried creating a reference to my viewpager acitivty from my fragment and calling the getItem method with the position of my new fragment but this doesn't work. This is my code right now. It is an activity, which creates the ViewPager. Inside of the ViewPager3 fragments are created and the TabLayoutis also created:

public class MyActivity extends AppCompatActivity {

    ViewPager viewPager;
    PagerAdapter pagerAdapter;

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

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        pagerAdapter = new PagerAdapter(getSupportFragmentManager(), MyActivity.this);
        viewPager.setAdapter(pagerAdapter);

        viewPager.setOffscreenPageLimit(3);

        // Give the TabLayout the ViewPager
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(viewPager);

        // Iterate over all tabs and set the custom view
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(pagerAdapter.getTabView(i));
        }
    }

    class PagerAdapter extends FragmentPagerAdapter {

        String tabTitles[] = new String[] { "FragmentA", "FragmentB", "FragmentC", };
        public Fragment[] fragments = new Fragment[tabTitles.length];
        Context context;

        public PagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public int getCount() {
            return tabTitles.length;
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return new MyFragmentA();
                case 1:
                    return new MyFragmentB(); //Inside of fragment B I will have a button. If that button is clicked this fragment needs to be replaced with a new fragment temporarliy, FRAGMENTD, which will have an anctionbar to be able to go back to fragment B. The tabs should show that the user is technically still in FragmentB tab. 
                case 2:
                    return new MyFragmentC();
            }

            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            return tabTitles[position];
        }

        public View getTabView(int position) {
            View tab = LayoutInflater.from(MyActivity.this).inflate(R.layout.custom_tab, null);
            TextView tv = (TextView) tab.findViewById(R.id.custom_text);
            tv.setText(tabTitles[position]);
            return tab;
        }

        //This populates your Fragment reference array:
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
            fragments[position]  = createdFragment;
            return createdFragment;
        }
    }


}

推荐答案

我认为这是有目的的?

public Fragment[] fragments = new Fragment[tabTitles.length];

您仅分配它,但从不使用它的元素.

You only assign it, but never use its elements.

您可以像这样使用它来懒散地创建片段

You could use it like so to lazily create the fragments

@Override
public Fragment getItem(int position) {
    Fragment f = fragments[position];

    if (f == null) {
        switch (position) {
            case 0:
                f = new MyFragmentA();
                break;
            case 1:
                f = new MyFragmentB();
                break;
            case 2:
                f = new MyFragmentC();
                break;
        }
        fragments[position] = f;
    }

    return f;
}

或者在这里看,这没有正确实现,因为super.instantiateItem(container, position)从未创建您想要的三个Fragment类中的任何一个.

Or looking here, this is not implemented correctly since super.instantiateItem(container, position) is never creating any of the three Fragment classes you want.

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
        fragments[position]  = createdFragment;
        return createdFragment;
    }

因此,我将在此处删除该方法

So, I would remove that method here

在FragmentB里面,尽管我会有一个按钮.现在,我需要能够单击此按钮,并创建一个新的片段FragmentD,它将取代FragmentB ...选项卡仍应显示FragmentB在所有这些过程中均处于活动状态.

Inside of FragmentB though I will have a button. I now need to be able to click this button and have a new fragment created, FragmentD, which will overtake FragmentB... The tabs should still show that FragmentB is the active tab during all of this.

您需要从FragmentB回调到Activity

You need some callback from FragmentB into the Activity

如何实现OnFragmentInteractionListener

然后,在活动"回调中创建并可以替换ViewPager的页面2".

Then, in the Activity callback, you create and can possibly "replace page 2" of the ViewPager.

此新片段需要带有一个带有后退箭头的操作栏,以便它可以返回到FragmentB.

This new fragment needs to have an action bar with the back arrow so it can go back to FragmentB.

您还需要在该界面中使用第二种方法,说出onBackPressed来重新创建FragmentB并将其加载到ViewPager

You also need a second method in that interface to say onBackPressed to re-create and load FragmentB over FragmentD in the ViewPager

这篇关于ViewPager-在片段页面中创建一个新片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆