fragment是3.0之后才有的,之前平板是3.0专用,后来手机和平板都用3.0
Activity:
package com.itheima.fragment;import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;public class MainActivity extends Activity { private Fragment03 fg3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fg3 = new Fragment03(); //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg3); //提交 ft.commit(); } public void click1(View v){ //把fragment01的界面显示至帧布局中 //创建fragment对象 Fragment01 fg1 = new Fragment01(); //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg1); //提交 ft.commit(); commit()之前没有调用addToBackStack(),那个fragment将会是destroyed;如果调用了addToBackStack(),这个fragment会是stopped,可以通过返回键来恢复。 } public void click2(View v){ //把fragment01的界面显示至帧布局中 //创建fragment对象 Fragment02 fg2 = new Fragment02(); //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg2); //提交 ft.commit(); } public void click3(View v){ //把fragment01的界面显示至帧布局中 //获取fragment管理器 FragmentManager fm = getFragmentManager(); //打开事务 FragmentTransaction ft = fm.beginTransaction(); //把内容显示至帧布局 ft.replace(R.id.fl, fg3); //提交 ft.commit(); }}
fragment1
package com.itheima.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment01 extends Fragment { //返回的view对象会作为fragment01的内容显示在屏幕上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //inflater是布局填充器 View v = inflater.inflate(R.layout.fragment01, null); return v; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); System.out.println("01create"); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("01start"); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("01resume"); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("01pause"); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("01stop"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("01destroy"); }}
fragment2
package com.itheima.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment02 extends Fragment { //返回的view对象会作为fragment02的内容显示在屏幕上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.fragment02, null); return v; }}
fragment3:
package com.itheima.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment03 extends Fragment { //返回的view对象会作为fragment03的内容显示在屏幕上 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View v = inflater.inflate(R.layout.fragment03, null); return v; } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); System.out.println("03create"); } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("03start"); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("03resume"); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("03pause"); } @Override public void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("03stop"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("03destroy"); }}