Step By Step系列之Fragments(一)
转载请注明:转自http://blog.csdn.net/dhongmo/article/details/8573876
1.什么是Fragments?
Fragments可以看成是Activity中UI的一部分,我们可以把多个Fragments放在同一个Activity里面,当然也在不同Activity里面重用一个Fragments。实际上Fragments是在3.0里为了解决平板和手机界面一致的问题而出现的,现在可以通过兼容库向下兼容3.0以下版本。Fragments跟Activity同样有生命周期,它必须嵌入到Activity里面,因为它的生命周期是寄托在Activity的生命周期上的。意思就是说,当Activity调用onCreate时,所有Fragments都会调用onCreate。我们可以在Activity运行的时候随时添加和删除Fragments,也可以把Fragments添加到Activity的back stack中,通过回退键回滚Fragments事务。
2.Fragments有什么用?
1.把逻辑处理分散在不同的Fragments里,根据不同的设备尺寸、分辨率等通过不同的Fragments组合达到适应不同设备的效果,在一个应用程序里,用户的操作体验不再是一成不变。下图左边是平板电脑,右边是手机。
2.把高复用性的界面部分写成Fragments,供不同Activity重复使用。如应用的Header和Footer。
3.生命周期
管理Fragments的生命周期与管理Activity的生命周期比较相似,下图右边是两个生命周期的对比,左边是Fragments的生命周期。
onCreate:系统创建Fragments调用此方法
onCreateView:当Fragment与View关联时调用此方法,可以理解为Activity里面的setContentView()方法
onActivityCreated:系统与之关联的Activity调用onCreate结束时会调用此方法
onStart:跟Activity的onStart同步,Fragments变成Visible前会调用此方法。与onStop成对
onResume:跟Activity的onResume同步,Fragments恢复为Visible时会调用此方法。与onPause成对
onPause:跟Activity的onPause同步,Fragments被部分挡住时调用此方法。
onStop:跟Activity的onStop同步,Fragments被完全被挡住或被系统杀掉时调用此方法与
onDestoryView:与Fragments关联的View被销毁时会调用此方法
onDestory:跟Activity的onDestory同步,Fragments被销毁时会调用此方法
onDetach:当Fragment与View解除关联时调用此方法
4.Step By Step
Step1 添加support library
通过SDK Manger下载support library
在项目里创建libs文件夹,把android-support-v4.jar放进libs,然后导入到项目里
Step2 创建XML文件
res/layout/main.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="fill_parent" android:layout_height="fill_parent" />
res/layout/firstfragments.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Fragments 1" android:textAppearance="@android:style/TextAppearance.Large" /> <Button android:id="@+id/Btn_firstfragments" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="to Fragments 2" android:textAppearance="@android:style/TextAppearance.Large" /> </LinearLayout>
res/layout/secondfragments.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Fragments 2" android:textAppearance="@android:style/TextAppearance.Large" /> <Button android:id="@+id/Btn_secondfragments" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="to Fragments 1" android:textAppearance="@android:style/TextAppearance.Large" /> </LinearLayout>
Step3 创建Activity和Fragment
package com.dong.fragmentsexample; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import com.dong.fragmentsexample.FirstFragments.onFirstListener; import com.dong.fragmentsexample.SecondFragments.onSecondListener; public class MainActivity extends FragmentActivity implements onFirstListener, onSecondListener { @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); setContentView(R.layout.main); // 1.获取FragmentManager FragmentManager fragmentManager = getSupportFragmentManager(); // 2.获取FragmentTransaction FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // 3.创建FirstFragments FirstFragments firstFragment = new FirstFragments(); // 4.添加firstFragment到FragmentActivity fragmentTransaction.add(R.id.fragment_container, firstFragment); // 5.提交事务 fragmentTransaction.commit(); } // 点击按钮时触发此函数 @Override public void onSecondSelected() { // TODO Auto-generated method stub FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FirstFragments firstFragment = new FirstFragments(); // 让firstFragment成为当前的Fragment fragmentTransaction.replace(R.id.fragment_container, firstFragment); fragmentTransaction.commit(); } // 点击按钮时触发此函数 @Override public void onFirstSelected() { // TODO Auto-generated method stub FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); SecondFragments secondFragment = new SecondFragments(); // 让secondFragment成为当前的Fragment fragmentTransaction.replace(R.id.fragment_container, secondFragment); fragmentTransaction.commit(); } }
package com.dong.fragmentsexample; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; public class FirstFragments extends Fragment { private Button frist_Btn; public onFirstListener mCallback; public interface onFirstListener { public void onFirstSelected(); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); // 确保继承OnHeadlineSelectedListener接口,否则抛出异常 try { mCallback = (onFirstListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub // 通过LayoutInflater动态添加布局文件 View view = inflater.inflate(R.layout.firstfragments, container, false); this.frist_Btn = (Button) view.findViewById(R.id.Btn_firstfragments); this.frist_Btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 点击按钮时回调onFirstSelected方法 mCallback.onFirstSelected(); } }); return view; } }
package com.dong.fragmentsexample; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; public class SecondFragments extends Fragment { private Button second_Btn; public onSecondListener mCallback; public interface onSecondListener { public void onSecondSelected(); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); // 确保继承OnHeadlineSelectedListener接口,否则抛出异常 try { mCallback = (onSecondListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub // 通过LayoutInflater动态添加布局文件 View view = inflater.inflate(R.layout.secondfragments, container, false); this.second_Btn = (Button) view.findViewById(R.id.Btn_secondfragments); this.second_Btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 点击按钮时回调onSecondSelected方法 mCallback.onSecondSelected(); } }); return view; } }
5.源代码
点击下载