1. The relationship between Fragment and Activity To understand dynamic management of Fragment, you must first understand the relationship between Fragment and Activity Fragment can be understood as dividing Activity into several fragments. Fragment is part of Activity, and its life cycle depends on Activity. It cannot exist independently. It is not difficult to understand that all Fragment management (finding, adding, deleting, replacing) should be done in the Activity that Fragment depends on, that is, Activity is the place for Fragment interaction. Don't try to manage another Fragment in a Fragment. Although it can achieve the required functions, it does not meet the specifications. To complete specific operations on Fragment, you need to use FragmentManager component. 2. Use FragmentManager component The key code for using the FragmentManager component is as follows: -
- FragmentManager fm= super .getSupportFragmentManager();
-
- fm.findFragmentById(R.id.fooler)
- fm.findFragmentByTag("tagName")
-
- fm.beginTransaction().add(R.id.content,contentFragment, "content" ).commit();
-
- fm.beginTransaction().replace(R.id.OldFragment, newFragment).commit();
-
- fm.beginTransaction().remove(R.id.myFragment).commit();
-
- f.setArguments(Bundle) method passes data
The above management Fragment code should be organized in the Activity to which the Fragment belongs. Let's take a look at the specific implementation through a case. 3. Implementation Case First, let's take a look at the application case (as shown in the figure). When you click the first area and the second area at the bottom, the center area is switched. Interface analysis: There are three areas: the header area TitleFragment, the center area is a FrameLayout layout that dynamically loads ContentFragment through code, and the bottom area FloorFragment 1. The layout of Activity is as follows - RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
- android:layout_width= "match_parent"
- android:layout_height= "match_parent" >
- <fragment android:id= "@+id/fgTitle"
- android:layout_width= "match_parent"
- android:layout_height= "45dp"
- android:name= "com.jereh.android.course.fragment.TitleFragment"
- />
- <FrameLayout android:id= "@+id/content"
- android:layout_width= "match_parent"
- android:layout_height= "match_parent"
- android:layout_below= "@id/fgTitle"
- />
- <fragment android:id= "@+id/floor"
- android:layout_width= "match_parent"
- android:layout_height= "wrap_content"
- android:layout_alignParentBottom= "true"
- android:orientation= "horizontal"
- android:name= "com.jereh.android.course.fragment.FloorFragment"
- />
- </RelativeLayout>
-
- Copy code
- 2. FloorFragment code
- Copy code
-
- public class FloorFragment extends Fragment implements OnClickListener {
- @Override
- public void onAttach(Activity activity) {
- super .onAttach(activity);
-
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
-
- View view=inflater.inflate(R.layout.floor_fragment, container, false );
- LinearLayout home=(LinearLayout)view.findViewById(R.id.home);
- LinearLayout list=(LinearLayout)view.findViewById(R.id.list);
- home.setOnClickListener( this );
- list.setOnClickListener( this );
- return view;
- }
-
- public interface OnContentFragmentListener{
- void setContentFragment(String param);
- }
- private OnContentFragmentListener onContentFragmentListener;
- public void setOnContentFragmentListener(
- OnContentFragmentListener onContentFragmentListener) {
- this .onContentFragmentListener = onContentFragmentListener;
- }
- @Override
- public void onClick(View view) {
-
- if (onContentFragmentListener!= null ){
- switch (view.getId()){
- case R.id.home:
-
- onContentFragmentListener.setContentFragment( "Main Panel Fragment" );
- break ;
- case R.id.list:
- onContentFragmentListener.setContentFragment( "list information" );
- break ;
- }
- }
- }
- }
3. ContentFragment code
- public class ContentFragment extends Fragment {
- private String title;
- public void setArguments(Bundle args) {
- title=args.getString( "title" );
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view=inflater.inflate(R.layout.content_fragment, container, false );
- if (title!= null )((TextView)view.findViewById(R.id.tvContent)).setText(title);
- return view;
- }
- }
4. DynaFragmentActivity code - public class DynaFragmentActivity extends FragmentActivity implements FloorFragment.OnContentFragmentListener {
- private ContentFragment contentFragment;
- private FloorFragment floor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super .onCreate(savedInstanceState);
- super .setContentView(R.layout.dyna_fragment_activity);
- initFragment();
- }
- private void initFragment(){
- floor=(FloorFragment) super .getSupportFragmentManager().findFragmentById(R.id.floor);
- floor.setOnContentFragmentListener( this );
- contentFragment= new ContentFragment();
- FragmentManager fm= super .getSupportFragmentManager();
- fm.beginTransaction().add(R.id.content,contentFragment, "content" ).commit();
- }
-
- public void setFragment(Fragment fragment){
- getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
- }
-
- @Override
- public void setContentFragment(String param) {
-
- Bundle bundle = new Bundle();
- bundle.putString( "title" , param);
- contentFragment= new ContentFragment();
- contentFragment.setArguments(bundle);
- super .getSupportFragmentManager().beginTransaction()
- .replace(R.id.content, contentFragment).commit();
- }
-
- }
|