Get fragment#39;s container view id(获取片段的容器视图 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?
我可以直接使用这个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取片段的容器视图 id
基础教程推荐
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
