里面ViewPager另一片段替换片段 [英] Replace fragment with another fragment inside ViewPager

查看:134
本文介绍了里面ViewPager另一片段替换片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ViewPager连同FragmentPagerAdapter举办三个不同的片段

I'm using a ViewPager together with a FragmentPagerAdapter to host three different fragments

[Fragment1][Fragment2][Fragment3]

我试图做到的,是成功地与一个全新的片段取代片段1,Fragment4,如果一个特定的任务成功完成。

What I'm trying to achieve is to successfully replace Fragment1 with a whole new fragment, Fragment4, if a specific task succeeds.

当我用..

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment1_layout_id, fragment4);
transaction.commit();

..片段被替换精美,Fragment4显示,而不是片段1。虽然只要我刷卡一路Fragment3再回到Fragment4,片段1又卷土重来。

..the fragment is replaced beautifully and Fragment4 is shown instead of Fragment1. Though as soon as I swipe all the way to Fragment3 and then back to Fragment4, Fragment1 has made a comeback.

话又说回来,如果我用..

Then again, if I use..

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.remove(fragment1);
transaction.commit();

..片段1被删除,当我回来的Fragment4是存在的。因此,这个解决方案的问题是,我无法找到一个方法来立即显示Fragment4的片段1被删除,即使我尝试:

..Fragment1 is removed and when I come back Fragment4 is there. So the problem with this solution is that I couldn't find a way to immediately show Fragment4 as Fragment1 is removed, even if I tried:

transaction.add(fragment4);
transaction.show(fragment4);

和这里的如何我FragmentPagerAdapter实现看起来像此刻,没有管理任何交易

And here's how my FragmentPagerAdapter implementation looks like at the moment, without any transaction managing:

public static class PagerAdapter extends FragmentPagerAdapter {

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

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

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = null;

        switch (position) {
        case 0:
            fragment = Fragment1.newInstance(context_);
            break;

        case 1:
            fragment = Fragment2.newInstance(context_);
            break;

        case 2:
            fragment = Fragment3.newInstance(context_);
            break;

        default:
            break;
        }

        return fragment;
    }
}

编辑。

所以好像我是不是我​​的问题完全清楚。我决定删除所有我设法先前创建的意大利面条,并试图指出,我已经离开他们关(见上面黑体字)。

So it seems like I wasn't totally clear with my question. I decided to remove all the spaghetti I managed to create earlier and tried to state that I had left them off (see bolded text above).

反正这里是pretty的很多东西我一直在努力做内部的getItem():

Anyway here's pretty much what I have been trying to do inside getItem():

@Override
public Fragment getItem(int position) {

    Fragment fragment = null;

    switch (position) {
    case 0:

        if (!app.isUserLoggedIn) {
            if (loginFragment_ == null) {
                loginFragment_ = LoginFragment.newInstance(context_);
                transaction_ = fragmentManager_.beginTransaction();

                if (logoutFragment_ != null) {
                    transaction_.remove(logoutFragment_);
                    logoutFragment_ = null;
                }

                transaction_.add(loginFragment_, "login");
                transaction_.commit();
            }

            if (fragmentManager_.findFragmentByTag("login") == null)
                fragment = LoginFragment.newInstance(context_);
            else
                fragment = fragmentManager_.findFragmentByTag("login");

        } else {
            if (logoutFragment_ == null) {                      
                logoutFragment_ = LogoutFragment.newInstance(context_);
                transaction_ = fragmentManager_.beginTransaction();

                if (loginFragment_ != null) {
                    transaction_.remove(loginFragment_);
                    loginFragment_ = null;
                }

                transaction_.add(logoutFragment_, "logout");
                transaction_.commit();
            }

            if (fragmentManager_.findFragmentByTag("logout") == null)
                fragment = LogoutFragment.newInstance(context_);
            else
                fragment = fragmentManager_.findFragmentByTag("logout");
        }
        break;

    case 1:
        fragment = HomeFragment.newInstance(context_);
        break;

    case 2:
        fragment = RegisterFragment.newInstance(context_);
        break;

    default:
        break;
    }

    return fragment;
}

通过这个code我什么也没有做,但如果有人看到我在做什么错了,想点我在正确的方向,我AP preciate了很多!

With this code I get nothing done, but if someone sees what I'm doing wrong and would like to point me in the correct direction, I'd appreciate it a lot!

推荐答案

尝试使用FragmentStatePagerAdapter

Try using FragmentStatePagerAdapter

<一个href="http://android-developers.blogspot.com/2011_08_01_archive.html">http://android-developers.blogspot.com/2011_08_01_archive.html

堂妹,你通过视图刷卡它会删除片段(也当你调用getItem),所以你不必自己显式删除它们。

coz it will remove the fragment as you swipe through the view(also when you call getitem), so you don't need to explicitly remove them yourself.

我一直有同样的问题,它为我工作。

I've been having the same problem it works for me.

我试试你的code和删除如果(片段!= NULL)块和它的作品。

I try your code and remove the if (fragment!=null) block and it works.

这篇关于里面ViewPager另一片段替换片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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