Android应用开发中Fragment的静态加载与动态加载实例

2019-12-10 18:59:54王旭

LeftFragment对应的布局文件:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
  android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/holo_orange_dark"
   android:orientation="vertical" >
 
   <Button
 android:id="@+id/previous_button"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/previous_button" />
 
   <Button
 android:id="@+id/next_button"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/next_button" />

   <Button
 android:id="@+id/exit_button"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/exit_button" />
 
 </LinearLayout>

 

RightFragment类:和LeftFragment类似

public class RightFragment extends Fragment
 {
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
     super.onCreate(savedInstanceState);
     System.out.println("RightFragment onCreate");
   }
   
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
     System.out.println("RightFragment onCreateView");
     return inflater.inflate(R.layout.rightfragment, container, true);
  }
  
   @Override
   public void onResume()
  {
    super.onResume();
     System.out.println("RightFragment onResume");
   }
   
   @Override
   public void onPause()
   {
     super.onPause();
     System.out.println("RightFragment onPause");
  }
   
   @Override
   public void onStop()
   {
     super.onStop();
     System.out.println("RightFragment onStop");
   }
 }

RightFragment对应的布局文件:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
 
   <TextView
 android:id="@+id/show_message"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@android:color/holo_blue_dark"
     android:text="@string/show_message" />
 
 </LinearLayout>