Fragment生命周期和动态切换



     左一:Fragment的生命周期                         左二:Activity的生命周期和Fragment的生命周期的比较
 

 

Fragment的生命周期只有开始创建和结束的地方与Activity不同;

    

2,生命周期的分析;

 

  1,创建时;

      onAttach() 

     onCreate()

     onCreateView()

    onActivityCreated()

 

2,对用户可见

   onstart()

  onResume()

 

3,进入后台模式时;

 

   onPause()

  onstop()

 

4,退出时;

 

  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy() 
  • onDetach()

 

5,横屏切换时;

  先销毁再重新创建  

    

       设置Fragment不重新创建需要在主配置文件设置

android:configChanges="orientation|screenSize"

 

 

 

  二;动态替换Fargment技术分析

 

点击第一个按钮的Fragment

 

第二个按钮的Fragment

 

 

    动态替换Fragment在android UI中使用的最广泛的

 

1,fragment1.xml文件内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"   
    android:background="#00FF00">
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textSize="25sp"
        />
</LinearLayout>

 

2,fragment2.xml内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#FF0000"
    >
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textSize="25sp"
        />
</LinearLayout>

 

3,使用java来操作fragment1.xml和fragment2.xml,并分别创建fragment1.java和fragment2.java

 

Fragment1 继承Fragment 重写onCreateView的方法

public class Frament1 extends Fragment {
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d("Fragment", "onCreateView");
		return inflater.inflate(R.layout.frament1, container,false);
	}
}

 分析Fragment1中的代码;

inflater.inflate(R.layout.frament1, container,false);获得布局文件,ViewGroup ,false

 

 

Fragment2中的代码;

public class Frament2 extends Fragment {


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.frament2, container,false);
	}
}

 

 

4,定义底部的图片按钮的布局文件 bottom_image.xml  每张图片添加监听事件

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:stretchColumns="0,1,2,3"
    >
    
    <TableRow 
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:background="#EEF990"
        
        >
        
        <ImageView 
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/word"
            android:onClick="change"
            />
         <ImageView 
            android:id="@+id/img2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/line"
            android:onClick="change"
            />
          <ImageView 
            android:id="@+id/img3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/saoma"
            android:onClick="change"
            />
           <ImageView 
            android:id="@+id/img4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/call"
            android:onClick="change"
            />
        
    </TableRow>

</TableLayout>

 

5,定义Fragment3来操作bottom_image.xml

public class Fragment3 extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//获得布局文件
		  View view=inflater.inflate(R.layout.bottom_image, container,false);
	 
		  return view;
	}
	
	
}

 

 

6,定义布局文件(main.xml)给启动的类(MainActivity1)操作  (重点)

 

   分析:动态切换的是中间位置,所以我们需要将中的使用布局表示;

<FrameLayout
       android:id="@+id/fragment2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/frament3"
       android:layout_below="@+id/frament1" />

 

 main.xml 文件的内容 

<?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:orientation="vertical" >
    
   <fragment 
     android:name="com.example.frament.Frament1" 
     android:id="@+id/frament1"
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"
     />
   
   <FrameLayout
       android:id="@+id/fragment2"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/frament3"
       android:layout_below="@+id/frament1" />

     <fragment
         android:id="@+id/frament3"
         android:name="com.example.frament.Fragment3"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true" />

</RelativeLayout>

 

 feagment 有一个id    

 name;com.example.frament.Fragment3表示的是java的类   此时的Fragment3表示一个布局文件

 

7,启动程序的类

package com.example.frament;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Display;
import android.view.View;

public class MainActivity1 extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
//		
//		//向活动添加碎片,根据屏幕的纵向和横向显示
//		//1,获取碎片管理器
//		FragmentManager fragment=getFragmentManager();
//		//2,碎片的显示需要使用FragmentTransaction类操作
//		FragmentTransaction transacction=fragment.beginTransaction();
//		//获取屏幕管理器和默认的显示
//		Display display=getWindowManager().getDefaultDisplay();
//		//判断横屏
//		if(display.getWidth()>display.getHeight()){
//			//获取java类
//			Frament1 frament1 =  new Frament1();
//			transacction.replace(android.R.id.content, frament1);
//		}else{
//			Frament2 frament2 =  new Frament2();
//			transacction.replace(android.R.id.content, frament2);
//		}
//		//使用FragmentTransaction必须要commit
//		transacction.commit();
	}

	
	//创建监听的方法
	public void change(View v){
		switch (v.getId()) {
		case R.id.img1:
			//获得Fragment的管理器
			FragmentManager manager=getFragmentManager();
			//获得事物的操作
			FragmentTransaction  transaction=manager.beginTransaction();
			//创建对象
			Frament1 fragment = new Frament1();
			//添加内容
			transaction.replace(R.id.fragment2, fragment);
			//提交
			transaction.commit();
			break;

		case R.id.img2:
			FragmentManager manager2=getFragmentManager();
			FragmentTransaction  transaction2=manager2.beginTransaction();
			Frament2 fragment2 = new Frament2();
			transaction2.replace(R.id.fragment2, fragment2);
			transaction2.commit();
			break;
		}
	}
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值