替换Viewpager中的片段/选项卡

时间:2012-04-02 17:52:51

标签: android android-fragments fragment android-viewpager

我目前正在尝试为未来的应用制作框架。我非常喜欢ActionBar和ViewPager,但错过了一个功能。我需要在运行时替换Fragment / Tab。

使用官方example,我希望看到类似于replaceTab()的内容,但无论我做什么,Fragment本身都不会更新。

4 个答案:

答案 0 :(得分:2)

您必须使用FragmentPagerAdapter来处理片段更改。有三件重要的事情:

  • 删除上一个片段(如果存在)。

  • 调用notifyDataSetChanged()刷新页面列表。

  • 如果要求旧片段,请在getItemPosition中返回POSITION_NONE。

这是我使用左页和右页的代码。我会根据用户的动态动态更改片段。

public class MyPager extends ViewPager {

    private MyPagerAdapter mMyPagerAdapter;

    public MyPager(Context context, FragmentActivity activity) {
        super(context);

        mMyPagerAdapter = new MyPagerAdapter(activity.getSupportFragmentManager());
        setAdapter(mMyPagerAdapter);

    }
    public void replaceLeftFragment(Fragment fragment) {
        mMyPagerAdapter.replaceLeftFragment(fragment);
    }
    public void replaceRightFragment(Fragment fragment) {
        mMyPagerAdapter.replaceRightFragment(fragment);
    }
}

public class MyPagerAdapter extends FragmentPagerAdapter {

    private FragmentManager mFragmentManager;
    private Fragment mLeftFragment;
    private Fragment mRightFragment;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        mFragmentManager = fm;
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return mLeftFragment;
        } else {
            return mRightFragment;
        }
    }

    @Override
    public int getCount() {
        final int nbLeft = (mLeftFragment == null) ? 0 : 1;
        final int nbRight = (mRightFragment == null) ? 0 : 1;
        return (nbLeft + nbRight);
    }

    public void replaceLeftFragment(Fragment fragment) {
        if (mLeftFragment != null) {
            mFragmentManager.beginTransaction().remove(mLeftFragment).commit();
        }
        mLeftFragment = fragment;
        notifyDataSetChanged();
    }
    public void replaceRightFragment(Fragment fragment) {
        if (mRightFragment != null) {
            mFragmentManager.beginTransaction().remove(mRightFragment).commit();
        }
        mRightFragment = fragment;
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        if ((object!=mLeftFragment) && (object!=mRightFragment)) {
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

}

答案 1 :(得分:1)

你试试:

fragmentTransaction.remove(yourframent);

然后:

fragmentTransaction.add(yournewfragment);

答案 2 :(得分:0)

如果您的Fragment是从布局文件创建的,则无法在运行时替换它。您只能成功替换以编程方式添加的片段。见Android: can't replace one fragment with another

答案 3 :(得分:0)

非常接近你想要的,我需要将片段(最后)添加到ViewPager,并且这样做:

    FragmentTransaction ft = fragmentManager.beginTransaction();
    new_fragment = Fragment.instantiate(activity, class_name, args);

    // The below code removes old fragment and adds the new one at the end.
    ft.attach(new_fragment);              // <- this adds the fragment at the end
    ft.detach(old_fragment);              // <- this removes old fragment

    // This should be what you're looking for when adding to an Activity instead of a ViewPager
    ft.replace(view_id, new_fragment);    // <- this is supposed to replace the fragment attached to view_id

    // Or if the replace does not work, this might work:
    ft.detach(old_fragment);
    ft.add(view_id, new_fragment);

    // Not to be forgotten: ;)
    ft.commit();

以上代码可能需要一些调整,可能只需要替换调用才能有效工作?

如果替换呼叫失败,您仍然可以分离所有碎片并按所需顺序重新附加。