Android:在动态创建的布局中查找 fragment

标签 android android-layout android-fragments

我正在尝试获取我创建的 LinearLayout 中的 fragment :

编辑:这是在扩展 ArrayAdapter<G> 的类中

LinearLayout newView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(inflater);
li.inflate(resource, newView, true);

布局中有一个名为 TableRowView 的 fragment :

编辑:我更正了 XML 语法

<fragment android:name ="mypackage.TableRowView"
    android:id="@+id/row"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我如何获得 newView的 fragment ?

提前致谢, --大卫

最佳答案

I am trying to get a hold of a fragment that is within a LinearLayout I have created

fragment 不在 LinearLayout 中。 fragment 不是 View,也不驻留在 ViewGroup 中。 onCreateView() 中的 fragment 创建的 View 将驻留在 ViewGroup 中。

I am trying to get a hold of a fragment that is within a LinearLayout I have created

尝试在 FragmentManager 上调用 findFragmentById(),前提是您在膨胀操作之前在 newView 上设置了一个 ID。

关于Android:在动态创建的布局中查找 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14899753/

相关文章:

java - 在 Intent 中传递整数和对象值不起作用

android - 在相对布局中将 TextView 对齐到 ImageButton 下方

android - 聚焦编辑文本时更改边框颜色

android - 如何设置对话框外边距? (安卓)

android - 验证在哪个 Activity 中附加了 fragment

java - 屏幕旋转后未调用 onSaveInstanceState

android - 如何将 Object[] 转换为 String[ ]

java - 如何使精简/专业版本的应用程序功能与引用的库略有不同

java - 在 fragment 之间使用 Otto 传递字符串

android - 如何在 ReccyclerView 的 onBindViewHolder 中设置文本?