Android:通过查找带有标签而不是ID的容器来添加片段 [英] Android: Add fragments by looking up container with tag instead of ID
问题描述
我正在使用for循环创建片段的多个实例.在每个片段中,我需要添加另一组子片段.为此,我需要找到正确的容器.如果使用容器的ID,则所有子片段都将添加到第一个父片段中,而不是它们自己的父片段中.
在我的主要片段中:
<.> <代码>用于(....){ParentFragment parentFragment = new ParentFragment();parentFragment.setSomeData(data);fragmentTransaction.add(R.id.main_fragment_container,parentFragment,data);}fragmentTransaction.commit();现在我有N个父片段.
在每个ParentFragment中:
@Overridepublic void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState){ChildFragment childFragment = new ChildFragment();childFragment.setSomeData(data);fragmentTransaction.add(R.id.parent_fragment_container,childFragment).commit();}
问题是, ChildFragment
的所有实例都添加到第一个ParentFragment中,因为"R.id.parent_fragment_container"是所有ParentFragment的常量.对于ParentFragment的每个实例,它都不会更改.
因为我对ParentFragment的每个实例都有唯一的标签,我可以使用此标签而不是 R.id.parent_fragment_container
来查找容器吗?
XML定义:
fragment_main.xml:
< LinearLayoutandroid:id ="@ + id/main_fragment_container"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:orientation ="vertical"></LinearLayout>
fragment_parent.xml:
<?xml version ="1.0" encoding ="utf-8"?>< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"android:id ="@ + id/parent_fragment_container"android:layout_width ="match_parent"android:layout_height ="wrap_content"android:orientation ="vertical"></LinearLayout>
Fragment
在另一个 Fragment
中应该与该父 Fragment
处理的 FragmentManager
–通过 getChildFragmentManager()
获得–而不是 Activity
的 FragmentManager
.
子级 FragmentManager
将仅搜索父级 Fragment
的 View
以获取给定的容器ID,因此最终不会所有第一个找到的容器中的子 Fragment
子元素,例如 Activity
的 FragmentManager
刚使用第一个 parent_fragment_container时发生的情况
会在 Activity
的整体层次结构中找到.
I am creating multiple instances of a fragment with a for loop. Within each fragment, I need to add another set of child fragments. In doing that, I need to find the correct container. If I use the ID of the container, all the child fragments gets added to the first parent fragment, instead of their own parent fragments.
In my main fragment:
for (....) {
ParentFragment parentFragment = new ParentFragment();
parentFragment.setSomeData(data);
fragmentTransaction.add(R.id.main_fragment_container, parentFragment, data);
}
fragmentTransaction.commit();
Now I have N Parent Fragments.
Within each ParentFragment:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
ChildFragment childFragment = new ChildFragment();
childFragment.setSomeData(data);
fragmentTransaction.add(R.id.parent_fragment_container, childFragment).commit();
}
The issue is, all instances of ChildFragment
gets added to the first ParentFragment because "R.id.parent_fragment_container" is a constant for all ParentFragment. It doesn't change for each instance of ParentFragment.
Because I have unique tag for each instance of ParentFragment, can I lookup the container using this tag instead of R.id.parent_fragment_container
?
XML definitions:
fragment_main.xml:
<LinearLayout
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
fragment_parent.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
Fragment
s inside another Fragment
should be handled with that parent Fragment
's FragmentManager
– obtained with getChildFragmentManager()
– rather than the Activity
's FragmentManager
.
The child FragmentManager
will search only the parent Fragment
's View
for the given container ID, so you won't end up with all of the child Fragment
s in the first found container, as was happening when the Activity
's FragmentManager
just used the first parent_fragment_container
it would find in the Activity
's overall hierarchy.
这篇关于Android:通过查找带有标签而不是ID的容器来添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!