Android是在Android 3.0 (API level 11)开始引入Fragment的。html
能够把Fragment想成Activity中的模块,这个模块有本身的布局,有本身的生命周期,单独处理本身的输入,在Activity运行的时候能够加载或者移除Fragment模块。android
能够把Fragment设计成能够在多个Activity中复用的模块。编程
当开发的应用程序同时适用于平板电脑和手机时,能够利用Fragment实现灵活的布局,改善用户体验。app
如图:ide
由于Fragment必须嵌入在Acitivity中使用,因此Fragment的生命周期和它所在的Activity是密切相关的。函数
若是Activity是暂停状态,其中全部的Fragment都是暂停状态;若是Activity是stopped状态,这个Activity中全部的Fragment都不能被启动;若是Activity被销毁,那么它其中的全部Fragment都会被销毁。布局
可是,当Activity在活动状态,能够独立控制Fragment的状态,好比加上或者移除Fragment。ui
当这样进行fragment transaction(转换)的时候,能够把fragment放入Activity的back stack中,这样用户就能够进行返回操做。this
使用Fragment时,须要继承Fragment或者Fragment的子类(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),因此Fragment的代码看起来和Activity的相似。spa
使用Support Library
Support Library是一个提供了API库函数的JAR文件,这样就能够在旧版本的Android上使用一些新版本的APIs。
好比android-support-v4.jar.它的完整路径是:
<sdk>/extras/android/support/v4/android-support-v4.jar.
它就提供了Fragment的APIs,使得在Android 1.6 (API level 4)以上的系统均可以使用Fragment。
为了肯定没有在旧版本系统上使用新版本的APIs,须要以下导入语句:
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager;
同时应该将上述的包拷入libs项目下的libs文件夹,而后在项目的Properties中添加:右键单击项目,选Properties,左边选Java Build Path,而后Add External JARs…,添加android-support-v4.jar.
当建立包含Fragment的Activity时,若是用的是Support Library,那么继承的就应该是FragmentActivity而不是Activity。
必须实现的三个回调函数
onCreate()
系统在建立Fragment的时候调用这个方法,这里应该初始化相关的组件,一些即使是被暂停或者被中止时依然须要保留的东西。
onCreateView()
当第一次绘制Fragment的UI时系统调用这个方法,必须返回一个View,若是Fragment不提供UI也能够返回null。
注意,若是继承自ListFragment,onCreateView()默认的实现会返回一个ListView,因此不用本身实现。
onPause()
当用户离开Fragment时第一个调用这个方法,须要提交一些变化,由于用户极可能再也不返回来。
实现Fragment的UI
提供Fragment的UI,必须实现onCreateView()方法。
假设Fragment的布局设置写在example_fragment.xml资源文件中,那么onCreateView()方法能够以下写:
public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.example_fragment, container, false); } }
onCreateView()中container参数表明该Fragment在Activity中的父控件;savedInstanceState提供了上一个实例的数据。
inflate()方法的三个参数:
第一个是resource ID,指明了当前的Fragment对应的资源文件;
第二个参数是父容器控件;
第三个布尔值参数代表是否链接该布局和其父容器控件,在这里的状况设置为false,由于系统已经插入了这个布局到父控件,设置为true将会产生多余的一个View Group。
当Fragment被加入Activity中时,它会处在对应的View Group中。
Fragment有两种加载方式:一种是在Activity的layout中使用标签<fragment>声明;另外一种方法是在代码中把它加入到一个指定的ViewGroup中。
另外,Fragment它能够并非Activity布局中的任何一部分,它能够是一个不可见的部分。这部份内容先略过。
加载方式1:经过Activity的布局文件将Fragment加入Activity
在Activity的布局文件中,将Fragment做为一个子标签加入便可。
如:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.news.ArticleListFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.news.ArticleReaderFragment" android:id="@+id/viewer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
其中android:name属性填上你本身建立的fragment的完整类名。
当系统建立这个Activity的布局文件时,系统会实例化每个fragment,而且调用它们的onCreateView()方法,来得到相应fragment的布局,并将返回值插入fragment标签所在的地方。
有三种方法为Fragment提供ID:
android:id属性:惟一的id
android:tag属性:惟一的字符串
若是上面两个都没提供,系统使用容器view的ID。
加载方式2:经过编程的方式将Fragment加入到一个ViewGroup中
当Activity处于Running状态下的时候,能够在Activity的布局中动态地加入Fragment,只须要指定加入这个Fragment的父View Group便可。
首先,须要一个FragmentTransaction实例:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
(注,若是import android.support.v4.app.FragmentManager;那么使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)
以后,用add()方法加上Fragment的对象:
ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();
其中第一个参数是这个fragment的容器,即父控件组。
最后须要调用commit()方法使得FragmentTransaction实例的改变生效。
两个Fragment类
package com.example.android.fragments; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class ArticleFragment extends Fragment { final static String ARG_POSITION = "position"; int mCurrentPosition = -1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // If activity recreated (such as from screen rotate), restore // the previous article selection set by onSaveInstanceState(). // This is primarily necessary when in the two-pane layout. if (savedInstanceState != null) { mCurrentPosition = savedInstanceState.getInt(ARG_POSITION); } // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); } @Override public void onStart() { super.onStart(); // During startup, check if there are arguments passed to the fragment. // onStart is a good place to do this because the layout has already been // applied to the fragment at this point so we can safely call the method // below that sets the article text. Bundle args = getArguments(); if (args != null) { // Set article based on argument passed in updateArticleView(args.getInt(ARG_POSITION)); } else if (mCurrentPosition != -1) { // Set article based on saved instance state defined during onCreateView updateArticleView(mCurrentPosition); } } public void updateArticleView(int position) { TextView article = (TextView) getActivity().findViewById(R.id.article); article.setText(Ipsum.Articles[position]); mCurrentPosition = position; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the current article selection in case we need to recreate the fragment outState.putInt(ARG_POSITION, mCurrentPosition); } }
package com.example.android.fragments; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; // The container Activity must implement this interface so the frag can deliver messages public interface OnHeadlineSelectedListener { /** Called by HeadlinesFragment when a list item is selected */ public void onArticleSelected(int position); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // We need to use a different list item layout for devices older than Honeycomb int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; // Create an array adapter for the list view, using the Ipsum headlines array setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines)); } @Override public void onStart() { super.onStart(); // When in two-pane layout, set the listview to highlight the selected list item // (We do this during onStart because at the point the listview is available.) if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) { getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception. try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Notify the parent activity of selected item mCallback.onArticleSelected(position); // Set the item as checked to be highlighted when in two-pane layout getListView().setItemChecked(position, true); } }
主Activity
package com.example.android.fragments; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; public class MainActivity extends FragmentActivity implements HeadlinesFragment.OnHeadlineSelectedListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check whether the activity is using the layout version with // the fragment_container FrameLayout. If so, we must add the first fragment if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of ExampleFragment HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } } public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Capture the article fragment from the activity layout ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // If the frag is not available, we're in the one-pane layout and must swap frags... // Create fragment and give it an argument for the selected article ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } } }
首页布局文件,分是否large平板模式
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
large平板
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
运用的知识,fragment间通讯
一、使用接口
二、重载onAttach方法
public void onAttach(Activity activity)
获取容器的Activity