xml layout file
fragment_list.xml (controls the content in the fragment)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="TextView" android:textColor="#000000" android:textSize="20sp"/> </RelativeLayout>
Fragment's Java code management class
package com.administrator.fragmentdemo; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * list fragment */ public class ListFragment extends Fragment { //create view @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { //new view View view = inflater.inflate(R.layout.fragment_list,container,false); TextView textView = view.findViewById(R.id.textView);//FindViewById needs to be performed according to the root view view textView.setText("hi"); return view; } @Override public void onAttach(Context context) { super.onAttach(context); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate (savedInstanceState); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onStart() { super.onStart(); } @Override public void onPause() { super.onPause(); } @Override public void onStop() { super.onStop(); } @Override public void onDestroyView() { super.onDestroyView (); } @Override public void onDestroy() { super.onDestroy (); } }
Set the displayable area for the fragment in the container activity_main.xml
Set up the two containers listContainer and detailContainer respectively
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.administrator.fragmentdemo.MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/listContainer" android:layout_width="150dp" android:layout_margin="1dp" android:layout_height="match_parent"> </LinearLayout> <LinearLayout android:id="@+id/detailContainer" android:layout_width="200dp" android:layout_margin="1dp" android:layout_height="match_parent"> </LinearLayout> </LinearLayout> </LinearLayout>
The Java code of the Activity that loads the Fragment
package com.administrator.fragmentdemo; import android.content.Context; import android.content.Intent; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); //1.container container 2.fragment 3.fragment-->container put the fragment into the container container //getSupportFragmentManager() gets a fragment container, beginTransaction() opens a thing, and add() puts the fragment in (associated with it). commit() submit to the system //R.id.listContainer, listContainer is the container id just set, and add associates the container with the fragment ListFragment fragment = new ListFragment (); getSupportFragmentManager() .beginTransaction() .add(R.id.listContainer,fragment) .commit(); getSupportFragmentManager() .beginTransaction() .add(R.id.detailContainer,new ListFragment()) .commit(); //new ListFragment() create a new fragment // // remove the fragment // getSupportFragmentManager() // .beginTransaction() // .remove(fragment) // .commit(); // //replace fragment // getSupportFragmentManager() // .beginTransaction() // .replace(R.id.listContainer,new ListFragment()) // .commit(); } }