我有一个ViewPager活动,ViewPager的每个页面都有一个片段。如何替换片段内的片段?
在我的Screen3Fragment
里面我有一个LinearLayout(lly_fragments
),我在其中显示一些其他片段。我开始显示片段Screen3_1
public class Screen3Fragment extends Fragment {
private FragmentManager manager;
private FragmentTransaction transaction;
public static Screen3Fragment newInstance() {
final Screen3Fragment mf = new Screen3Fragment();
return mf;
}
public Screen3Fragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_screen3, container, false);
Screen3_1Fragment frag31 = new Screen3_1Fragment();
manager = getChildFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.lly_fragments,frag31,"frag31");
transaction.addToBackStack("frag31");
transaction.commit();
return v;
}
}
这工作正常没有问题。从frag31
(它位于Screen3Fragment内部)开始,我想调用fragt32
,因为我做了以下操作。
public class Screen3_1Fragment extends Fragment {
private ImageButton imgbt_timer;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_screen3_1,container,false);
imgbt_timer = (ImageButton) v.findViewById(R.id.bT_scr31_timer);
imgbt_timer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getChildFragmentManager().beginTransaction()
.replace(R.id.lly_fragments, new Screen3_2Fragment(), "frag32")
.commit();
}
});
return v;
}
}
正如我在其他的答案读,行transaction.replace
应该做的伎俩并更换由给定frag32
现有frag31
同一给定容器内lly_fragments
。
但是,我得到java.lang.IllegalArgumentException: No view found for id.....
我不知道为什么。
看看View.invalidate()。 https://developer.android.com/reference/android/view/View.html#invalidate() 不知道,但也许这可能有所帮助。 –
使用片段管理器,而不是子片段管理器。我想如果你使用子管理器,你应该在父代片段中使用view id。 –
@FlorescuGeorgeCătălin,谢谢,我将不胜感激更详细的答案,我应该在哪里使用它?在我的'Screen3Fragment' ??中,在我的'Screen3_1Fragment' ??在他们两个??中,为什么?我读到,当使用片段内的片段时,您称为儿童管理器 – codeKiller