获取片段的容器视图 id

Get fragment#39;s container view id(获取片段的容器视图 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)