获取碎片的容器视图id [英] Get fragment's container view id

查看:413
本文介绍了获取碎片的容器视图id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段加入使用

transaction.add(R.id.content, fragment, null);

和我需要从这个开始新的片段。但要做到这一点,我需要知道的第一个片段的容器视图的ID(R.id.content在我的情况)。我怎样才能得到呢?

and I need to start new fragment from this one. But to do this I need to know first fragment's container view id (R.id.content in my case). How can I get this?

我就可以直接使用这个号码,不过我想片段不应该知道这样那样的约父活动的详细信息。例如这将是不可能使用该片段在这种情况下,另一种活性

I can just use this id directly but I suppose fragment shouldn't know such kind of details about parent activity. For example it will be impossible to use this fragment in another activity in this case.

可能是开始的片段,从另外一个是不好的做法,所有的片段处理逻辑应该由活动本身的处理?但是创建开始互相片段不错序列显得相当有用的(例如detalView-> moreDetailView-> evenMoreDetailView)。

May be "starting" fragment from another one is a bad practice and all fragment handling logic should be handled by activity itself? But creating nice sequences of fragments starting each other seems quite useful (for example detalView->moreDetailView->evenMoreDetailView).

推荐答案

您可以通过调用访问容器的id

You can access the container's id by calling

((ViewGroup)getView().getParent()).getId();

这篇关于获取碎片的容器视图id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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