如何从片段本身替换活动的片段?片段

由网友(北岸初晴)分享简介:我的应用程序都有其活动里面的片段。我想从目前的片段本身另一个编程方式替换片段。My application has a Fragment inside its Activity. I would like to programmatically replace the fragment by another one...

我的应用程序都有其活动里面的片段。我想从目前的片段本身另一个编程方式替换片段。

My application has a Fragment inside its Activity. I would like to programmatically replace the fragment by another one from the current fragment itself.

例如,如果我点击片段内的一个按钮,该片段应用另一个替换,但活性应保持相同。

For example, if I click on a button inside the fragment, the fragment should be replaced with another one, but the activity should remain the same.

这可能吗?如果是的话,该怎么办呢?

Is it possible? If so, how to do it?

推荐答案

这其实很容易调用活动来代替片段。

It's actually easy to call the activity to replace the fragment.

您需要转换getActivity():

You need to cast getActivity():

((MyActivity) getActivity())

然后就可以调用从MyActivity方法,例如:

Then you can call methods from MyActivity, for example:

((MyActivity) getActivity()).replaceFragments(Object... params);

当然,这是假设你有一个replaceFragments()方法,在你的活动处理该片段取代过程。

Of course, this assumes you have a replaceFragments() method in your activity that handles the fragment replace process.

阅读全文

相关推荐

最新文章