机器人,动态更改标签内的片段 [英] android, dynamically change a fragment inside a tab

查看:121
本文介绍了机器人,动态更改标签内的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着改变使用 FragmentTabHost getSupportFragmentManager()创建一个选项卡的内容,但我不知道该怎么做。
以下是我有:

I'm trying to change the content of a tab that was created using FragmentTabHost and getSupportFragmentManager(), but I'm not sure how to do it. Here is what I have:

mTabHost = new FragmentTabHost(this);
    setContentView(mTabHost);

    mTabHost.setup(this, getSupportFragmentManager(),R.id.menu_settings);
    mTabHost.addTab(mTabHost.newTabSpec("A").setIndicator("A"),
            A.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("B").setIndicator("B"),
            B.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("C").setIndicator("C"),
            C.class, null);

加载每个类扩展片段和 onCreateView 他们夸大它们的布局。

问题是,我在他们里面(例如的A.class)的一个对话,并根据对话的回应,我需要导航到一个片段D,将它放在扣C
我应该怎么办呢?
我可以从该对话框创建的选项卡,并指定片段的活性沟通,但我不知道如何改变这是一个选项卡里面的片段(在这种情况下,C)。

the issue is that I have a dialog inside one of them ( for example A.class ), and depending of the response of the dialog, I need to navigate to a fragment D, placing it on tab C How should I do this? I could communicate from the dialog to the activity that creates the tabs and specify the fragments, but I don't know how to change the fragment that is inside a tab ( in this case C ).

作为总结,我需要C片段更改为D,其中C放置一个选项卡中。
我使用的是支持库。

As a summary, I need to change fragment C to D inside a tab where C was placed. I'm using the support library.

谢谢!

推荐答案

创建该类的行为像一个片段的容器。

Create a class acts like an fragment container.

例如:

public class FragmentContainer extends SherlockFragment implements OnBackStackChangedListener {
public static final String PARAM_CONTENT_FRAGMENT = "param_content_fragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.frag_container, null);
}

public void replaceContent(Class<? extends Fragment> clz, Bundle args) {
    FragmentTransaction tx = getChildFragmentManager().beginTransaction();

    tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

    // save
    Fragment curFrag = getChildFragmentManager().findFragmentById(R.id.fragment_content);
    tx.addToBackStack(curFrag.getClass().getSimpleName());

    // change
    try {
        Fragment newFragment = clz.newInstance();
        newFragment.setArguments(args);
        tx.replace(R.id.fragment_content, newFragment, clz.getClass().getSimpleName());
        tx.commit();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

@Override
public void onResume() {
    super.onResume();
    Fragment f = getChildFragmentManager().findFragmentById(R.id.fragment_content);
    if (f == null) {
        Class<? extends Fragment> claz = (Class<? extends Fragment>) getArguments().getSerializable(
                PARAM_CONTENT_FRAGMENT);
        FragmentTransaction tx = getChildFragmentManager().beginTransaction();
        try {
            f = claz.newInstance();
            f.setTargetFragment(this, 0);
            tx.add(R.id.fragment_content, f);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }
}

下面几个关键点:


  1. 初始化与FragmentContainer.class第三个选项卡,并提供C.class作为一个片段参数。 (参数关键是PARAM_CONTENT_FRAGMENT)

  1. Init the third tab with FragmentContainer.class and provide C.class as an fragment arguments. (argument key is PARAM_CONTENT_FRAGMENT)

onCreateView()
只要创建ID为@ + ID / fragment_content一个的FrameLayout,这是我们放置子片段。

onCreateView() Just create a FrameLayout with id @+id/fragment_content, this is where we place child fragment.

onResume()
放置子片段插入的FrameLayout如果不存在。

onResume() Place child fragment into FrameLayout if not exists.

replaceContent()
调用此方法时,片段-C要改变它自身进行分段-D。

replaceContent() Call this method When Fragment-C wants to change it-self to Fragment-D.

在Frament-C,例如:

In Frament-C, for example:

((FragmentContainer)getParentFragment() ).replaceContent( D.class, null );

这篇关于机器人,动态更改标签内的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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