问题描述
我添加了一个片段
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?
我可以直接使用这个 id,但我想片段不应该知道关于父活动的这种细节.例如,在这种情况下,将无法在另一个活动中使用此片段.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!