用 ViewPager 中的另一个片段替换片段 [英] Replace fragment with another fragment inside ViewPager

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

问题描述

我使用 ViewPager 和 FragmentPagerAdapter 来托管三个不同的片段

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

[Fragment1][Fragment2][Fragment3]

如果特定任务成功,我想要实现的是用全新的片段 Fragment4 成功替换 Fragment1.

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 而不是 Fragment1.尽管我一直滑动到 Fragment3 然后回到 Fragment4,Fragment1 又卷土重来.

..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();

..Fragment1 被删除,当我回来时 Fragment4 在那里.所以这个解决方案的问题是我无法找到一种方法来立即显示 Fragment4,因为 Fragment1 已被删除,即使我尝试过:

..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).

无论如何,这几乎是我在 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;
}

使用此代码我什么也没做,但是如果有人看到我做错了什么并想指出我正确的方向,我将不胜感激!

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

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.

我尝试您的代码并删除 if (fragment!=null) 块,它可以工作.

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

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

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