Normally we can add the multiple Fragment inside the FragmentActivity, But to add Fragment from inside the another Fragment is use the following way to achieve the task.
here i have added the Fragment in the Tab2Fragment Fragment class,with out changing the Tab also using the following code
1.Tab2Fragment.java
2.Tab2InnerFragment.java
3.BaseContainerFragment.java
here Full Source code
here i have added the Fragment in the Tab2Fragment Fragment class,with out changing the Tab also using the following code
1.Tab2Fragment.java
public class Tab2Fragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab2_view, null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(); } private void initView() { Button button = (Button) getView().findViewById(R.id.tab2_btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { replaceFragment(); } }); } private void replaceFragment() { ((BaseContainerFragment)getParentFragment()).replaceFragment(new Tab2InnerFragment(), true); } }
2.Tab2InnerFragment.java
public class Tab1InnerFragment extends Fragment { private boolean mIsViewInited; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.inner_tab1_view, null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); initView(); } private void initView() { Button button = (Button) getView().findViewById(R.id.back); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goBack(); } }); } private void goBack() { ((BaseContainerFragment)getParentFragment()).popFragment(); } }
3.BaseContainerFragment.java
public class BaseContainerFragment extends Fragment { public void replaceFragment(Fragment fragment, boolean addToBackStack) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); if (addToBackStack) { transaction.addToBackStack(null); } transaction.replace(R.id.container_framelayout, fragment); transaction.commit(); getChildFragmentManager().executePendingTransactions(); } public boolean popFragment() { boolean isPop = false; if (getChildFragmentManager().getBackStackEntryCount() > 0) { isPop = true; getChildFragmentManager().popBackStack(); } return isPop; } }
here Full Source code
No comments:
Post a Comment