待续//参考/reference/android/app/Fragment.html
/guide/topics/fundamentals/fragments.html
Fragment用来描述一些行为或一部分用户界面在一个Activity中,我们可以通过合并多个fragment在一个单独的activity中建立多个UI面板,也可以同时重用fragment在多个activity中。可以认为fragment作为activity的一节模块,fragment有自己的生命周期,接收自己的输入事件,可以从运行中的activity中添加/移除。
创建一个fragment必须创建Fragment的子类或者存在的子类
当系统在首次绘制用户界面调用fragment时,必须通过onCreateView在你的fragment画一个UI然后返回一个View(null表示没有fragment)
当fragment创建时被调用,要用到onCreate()初始化一些实用的空间,比如再fragment暂停或停止时需要恢复的
public class MyFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.myfragment, container, false); } }
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are: onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreaate. onStart() makes the fragment visible to the user (based on its containing activity being started). onResume() makes the fragment interacting with the user (based on its containing activity being resumed). As a fragment is no longer being used, it goes through a reverse series of callbacks: onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state. onDetach() called immediately prior to the fragment no longer being associated with its activity.
Layout
在xml文件里,可以布局多个fragment,也可以布局layout之后通过替换得到fragment
比如
<LinearLayout android:orientaion="horizontal">
<fragment android:name="com.example.mytitlefragment"/>
<fragment android:name="com.example.mydetailfragment"/>
</LinearLayout>
<LinearLayout android:orientaion="horizontal"> <fragment class="com.example.FragmentLayout$TitlesFragment"/> <FrameLayout android:id="@+id/details"/> </LinearLayout>
android:name= "myclass"
class="myclass.myStaticSubclass"
另外注意第二个xml文件中,details通过FrameLayout布局,之后在运行时替换(programmatically add the fragment to an existing ViewGroup)
管理fragment,必须要用到FragmentManager.
FragmentManager提供了几个与fragments交互的APIs,比如finding fragments in the activity还有popping fragment off the back stack to restore their previous state.
Transaction
为了运行一次transaction,比如add or remove a fragment,必须创建一个FragmentTransaction。之后可以调用add() remove()或者replace()。
每次applied这些transaction侯,必须call commit()之后系统才会应用这些fragment transaction到activity中去。
和Activity互相通讯
Fragment is implemented as an object that's independent from an Activity and can be used inside multiple activities.
Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout:
Although a Fragment is implemented as an object that's independent from an Activity and can be used inside multiple activities, a given instance of a fragment is directly tied to the activity that contains it. Specifically, the fragment can access the Activity instance with getActivity() and easily perform tasks such as find a view in the activity layout: View listView = getActivity().findViewById(R.id.list); Likewise, your activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag(). For example: ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);