我想在android中实现以下概念: -
Tab A -> Fragment A -> Fragment 1 -> Fragment 2
Tab B -> Fragment B
Tab C -> Fragment C
Tab D -> Fragment D
我在视图寻呼机上添加了标签,并且能够实现直到
Tab A -> Fragment A
Tab B -> Fragment B
Tab C -> Fragment C
Tab D -> Fragment D
我试过
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, fragment1);
ft.addToBackStack(null);
ft.commit();
但无法在Fragment A
上添加其他片段。
它会在Fragment A
上添加片段1,但Fragment A
在后台仍然可见。所以请提供一些建议来完成上述任务。
在此先感谢。
答案 0 :(得分:0)
这是我的。希望它有用。
首先,你需要为4个标签A,B,C,D创建ContentFragment,如下所示:
public class ContentFragmentA extends BaseFragment {
private static Context context;
@SuppressWarnings("static-access")
public ContentFragmentA(Context context) {
super();
this.context = context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.content_frame_a, container, false);
this.setFragmentManager(getChildFragmentManager());
this.setContainerId(R.id.content_frame_a);
this.replaceFragmentIntoStack(new FragmentOne(context), "FragmentOne");
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}}
然后,创建content_fragment_a.xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_frame_a"
android:layout_width="match_parent"
android:layout_height="match_parent" >
然后,对于每个新片段,您应该扩展 BaseFragment 。如果您想将新片段放入选项卡,只需使用 addFragmentIntoStack()或 replaceFragmentIntoStack()。
public class BaseFragment extends Fragment{
private FragmentManager fragmentManager;
private int containerId;
private boolean doubleBackToExit = false;
/**
* @param containerId the containerId to set
*/
public void setContainerId(int containerId) {
this.containerId = containerId;
}
/**
* @param fragmentManager the fragmentManager to set
*/
public void setFragmentManager(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
}
/**
*
*/
public BaseFragment() {
super();
// TODO Auto-generated constructor stub
BarddyApplication.getInstance().baseFragment = BaseFragment.this;
}
public void addFragmentIntoStack(BaseFragment newContent, String tagName) {
newContent.setFragmentManager(fragmentManager);
newContent.setContainerId(containerId);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(containerId, newContent, tagName);
fragmentTransaction.addToBackStack(tagName);
fragmentTransaction.commitAllowingStateLoss();
}
public void popFragment() {
fragmentManager.popBackStack();
}
public void replaceFragmentIntoStack(BaseFragment newContent, String tagName) {
newContent.setFragmentManager(fragmentManager);
newContent.setContainerId(containerId);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId,newContent,tagName);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commitAllowingStateLoss();
}}