Android之Fragment学习笔记① Android Fragment完全解析,关于碎片你所需知道的一切 四. 如何动态何切换Fragment
一. 什么是Fragment
Fragment(碎片)就是小型的Activity,它是在Android3.0时出现的。Fragment是表现Activity中UI的一个行为或者一部分。
可以把fragment想象成activity的一个模块化区域,有它自己的生命周期,接收属于它自己的输入事件,并且可以在activity运行期间添加和删除(有点像一个可以在不同的activity中重用的“子Activity”)。
Fragment必须被嵌入到一个activity中。它们的生命周期直接受其宿主activity的生命周期影响。当一个activity正在运行时,就可以独立地操作每一个Fragment,比如添加或删除它们。
Fragment可以定义自己的布局、生命周期回调方法,因此可以将fragment重用到多个activity中,因此可以根据不同的屏幕尺寸或者使用场合改变fragment组合。
二. 如何创建一个Fragment
1、为Fragment定义一个布局
2、定义类继承Fragment
3、重写类中的onCreateView方法,返回一个View对象作为当前Fragment的布局。
fragment第一次绘制它的用户界面的时候,系统会调用onCreateView()方法。为了绘制fragment的UI,此方法必须返回一个作为fragment布局的根的view。如果fragment不提供UI,可以返回null。
代码:如Fragment01和Fragment02所示。
三. 如何将Fragment添加到Activity
Activity必须在清单文件中进行声明,但是Fragment不需要,Fragment只需要在Activity的布局文件layout_main.xml中声明就可以了。
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:baselineAligned="false" > 5 6 <fragment 7 android:id="@+id/fragment1" 8 android:name="com.example.fragmentdemo.Fragment01" 9 android:layout_width="0dip" 10 android:layout_height="match_parent" 11 android:layout_weight="1" /> 12 13 <fragment 14 android:id="@+id/fragment2" 15 android:name="com.example.fragmentdemo.Fragment02" 16 android:layout_width="0dip" 17 android:layout_height="match_parent" 18 android:layout_weight="1" /> 19 20 </LinearLayout>
Fragment的代码:
1 package com.example.fragmentdemo; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 /** 10 * Created by gary on 2016/4/12. 11 */ 12 public class Fragment01 extends Fragment { 13 @Override 14 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 15 //return super.onCreateView(inflater, container, savedInstanceState); 16 return inflater.inflate(R.layout.fragment1,container,false); 17 } 18 } 19 20 package com.example.fragmentdemo; 21 22 import android.app.Fragment; 23 import android.os.Bundle; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 28 /** 29 * Created by gary on 2016/4/12. 30 */ 31 public class Fragment02 extends Fragment { 32 @Override 33 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 34 //return super.onCreateView(inflater, container, savedInstanceState); 35 return inflater.inflate(R.layout.fragment1,container,false); 36 } 37 }
Fragment的布局文件
1 fragment1.xml 2 <?xml version="1.0" encoding="utf-8"?> 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:background="#00ff00"> 7 8 <TextView 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="第一个Fragment" 12 android:textColor="#ff0000" 13 android:textSize="25sp"/> 14 15 </LinearLayout> 16 17 ------------------------ 18 fragment2.xml 19 <?xml version="1.0" encoding="utf-8"?> 20 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 21 android:layout_width="match_parent" 22 android:layout_height="match_parent" 23 android:background="#ff0000"> 24 25 <TextView 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:text="第二个Fragment" 29 android:textColor="#00ff00" 30 android:textSize="25sp"/> 31 32 </LinearLayout>
Activity代码:
1 package com.example.fragmentdemo; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 6 /** 7 * Created by gary on 2016/4/12. 8 */ 9 public class MainActivity extends Activity { 10 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.layout_main); 16 } 17 }
效果:
http://i.cnblogs.com/EditPosts.aspx?postid=5380639
注意:代码中的四个属性是必须的要给的,“android:name”属性:指定了在layout中实例化的Fragment类是哪个。
当系统创建这个activitylayout时,它实例化每一个在layout中指定的Fragment,并调用它们的onCreateView()方法,来获取每一个Fragment的layout,系统将从Fragment返回的View直接插入到<fragment>元素所在的地方。
四. 如何动态何切换Fragment
要在Activity中管理Fragment,需要四步
1. 获取FragmentManger对象,在Activity可以通过getFragementManager()来获取实例。
1 //1.获取Fragment管理器对象 2 FragmentManager manager = getFragmentManager();
2.开启一个事务,通过调用beginTransaction方法开启。
1 //2. 开启事务 2 FragmentTransaction transaction = manager.beginTransaction();
3.向容器中加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。
1 //3. 将FrameLayout控件替换成Fragment对象 2 transaction.replace(R.id.frame, new GamesFragment());
4. 提交事务,调用commit方法提交。
1 //4. 提交事务 2 transaction.commit();
案例:点击不同的按钮切换到不同的Fragment进行显示。
具体实现步骤:
1. 设置布局文件layout_main.xml中添加三个按钮用于切换Fragment,并在按钮下方添加一个FrameLayout用来替换成相应的Fragment布局。
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 <LinearLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="horizontal"> 10 11 <Button 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:layout_weight="1" 15 android:text="新闻" 16 android:onClick="news"/> 17 <Button 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:text="体育" 22 android:onClick="sports"/> 23 <Button 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:layout_weight="1" 27 android:text="游戏" 28 android:onClick="games"/> 29 30 </LinearLayout> 31 <FrameLayout 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content" 34 android:id="@+id/frame"/> 35 36 </LinearLayout>
2. 创建Fragment的布局文件,fragment_news.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:text="新闻栏目" 10 android:textSize="28sp" 11 android:textColor="#0000ff"/> 12 </LinearLayout>
fragment_sports.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:text="体育栏目" 9 android:textSize="28sp" 10 android:textColor="#ff0000"/> 11 </LinearLayout>
fragment_games.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:text="游戏栏目" 9 android:textSize="28sp" 10 android:textColor="#00ff00"/> 11 </LinearLayout>
3. 创建三个Fragment,SportsFragment、NewsFragment、GameFragment。
1 public class NewsFragment extends Fragment { 2 @Override 3 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 4 // return super.onCreateView(inflater, container, savedInstanceState); 5 return inflater.inflate(R.layout.fragment_news,null); 6 } 7 }
SportFragment和GamesFragment中代码和NewsFragment相似。
4. 添加切换Fragment的逻辑,分别添加新闻、体育、游戏的点击事件。
1 public class MainActivity extends Activity { 2 @Override 3 protected void onCreate(Bundle savedInstanceState) { 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.layout_main); 6 } 7 8 public void news(View v){ 9 //获取Fragment管理器对象 10 FragmentManager manager = getFragmentManager(); 11 //开启事务 12 FragmentTransaction transaction = manager.beginTransaction(); 13 //将FrameLayout控件替换成Fragment对象 14 transaction.replace(R.id.frame, new NewsFragment()); 15 //提交事务 16 transaction.commit(); 17 } 18 public void games(View v){ 19 //获取Fragment管理器对象 20 FragmentManager manager = getFragmentManager(); 21 //开启事务 22 FragmentTransaction transaction = manager.beginTransaction(); 23 //将FrameLayout控件替换成Fragment对象 24 transaction.replace(R.id.frame, new GamesFragment()); 25 //提交事务 26 transaction.commit(); 27 } 28 public void sports(View v){ 29 //获取Fragment管理器对象 30 FragmentManager manager = getFragmentManager(); 31 //开启事务 32 FragmentTransaction transaction = manager.beginTransaction(); 33 //将FrameLayout控件替换成Fragment对象 34 transaction.replace(R.id.frame, new SportsFragment()); 35 //提交事务 36 transaction.commit(); 37 } 38 }
sports()方法、games()方法同上
5. 运行效果