Android Fragment

1>Fragment可以作为activity界面的一部分组成出现。
2>可以在一个activity中同时出现多个fragment,并且一个fragment也可以在多个activity中使用。
3>在activity运行过程中,可以添加、移除或者替换Fragment。
4>Fragment可以响应自己的输入事件,并且有自己的生命周期,他们的生命周期会受宿主activity的生命周期的影响。
 
Fragment第一次绘制它的用户界面的时候,系统会调用onCreateView()方法为了绘制Fragment的UI,此方法必须返回view,如果不显示UI,返回NULL即可。
 
Fragment的加载方式:
      在activity的layout文件中声明Fragment,需要特别注意的是<Fragment >中的android:name属性指定了在layout中实例化的Fragment类。
标识Fragment的方法:
     android:id 属性提供一个唯一的ID
     android:tag 属性提供一个唯一的字符串
 
Fragmen 静态加载
 
mian.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" >
   
        <fragment
           android:id="@+id/fragment"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:name="com.example.fragment.MyFragment"
           />
   
</LinearLayout>
 
Fragment.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" >
   
        <TextView
               android:id="@+id/textview"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
           />
        <Button
               android:id="@+id/button"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
           />
   
</LinearLayout>
 
 
public class MyFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
               /*
               * layout布局文件转换为view对象
               * resource:fragment需要加载的布局文件
               * root:加载layout的父 viewgroup
               * attachToRoot:  boolean   false,不返回父 viewgroup
               */
              View view=inflater.inflate(R.layout. fragment, container, false);
              TextView textView=(TextView) view.findViewById(R.id. textview);
               return view;
       }
}
 
public class MainActivity2 extends Activity{
        private TextView textView ;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout. mian2);
               textView=(TextView) findViewById(R.id. textview);
              Button button=(Button) findViewById(R.id. button);
              button.setOnClickListener( new OnClickListener() {
                      @Override
                      public void onClick(View v) {
                            textView.setText( "az"); 
                     }
              });
       }
}
 
public class MainActivity extends ActionBarActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout. activity_main);
              Intent intent= new Intent(this ,MainActivity2.class);
              startActivity(intent);
       }
}
 
Fragment动态加载
     撰写代码将fragment添加到一个activity layout中
     add(): 添加一个Fragment(指定要添加的fragment和插入的view)与此类似的还有remove()、替换().
 
处理Fragment事务
     根据用户的交互情况,对fragment进行添加、移除、替换,以及执行其他动作,提交给activity的每一套变化被称作一个事务。
          FragmentManager fragmentManager=getFragmentManager();
          FragmentTranSactioin beginTransaction=fragmentManager.beginTransaction();
     每一个事务都是同事执行一套变化,可以在一个事务中设置你想要执行的变化、包括add()、remove()、replace(),然后提交给activity,必须调用commit()方法。
     如果允许用户通过按下BACK按钮返回到前一个Fragment状态,调用commit()之前可以加入addToBackStack()方法。
           
   MyFragment2 fragment2=new MyFragment2();
   FragmentManager fragmentManager=getFragmentManager();
   FragmentTransaction beginTransaction=fragmentManager.beginTransaction();
   beginTransaction.add(R.id. from, fragment2);
   beginTransaction.addToBackStack( null);     //允许物理按钮回退
   beginTransaction.commit();
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools= "http://schemas.android.com/tools"
    android:layout_width= "match_parent"
    android:layout_height= "match_parent" >
       <RelativeLayout
           android:id="@+id/from"
           ></RelativeLayout>
</LinearLayout>
 
 
Fragment的生命周期
     
public class MyFragment extends Fragment {
 
        /**
        * 每次穿件都会绘制fragment的view组件式回调该方法
        */
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
               // TODO Auto-generated method stub
               return super .onCreateView(inflater, container, savedInstanceState);
       }
        /**
        * 当fragment被添加到activity时回调该方法,只会回调一次
        */
        @Override
        public void onAttach(Activity activity) {
               super.onAttach(activity);
       }
       
        /**
        * 创建fragment时回调,只会回调一次
        */
        @Override
        public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
       }
       
        /**
        * 当fragment所在activity启动完成后调用
        */
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
               super.onActivityCreated(savedInstanceState);
       }
       
        /**
        * 启动fragment
        */
        @Override
        public void onStart() {
               super.onStart();
       }
       
        /**
        * 回复fragment时会被回调,调用 onstart()方法后面一定会调用onResume()方法
        */
        @Override
        public void onResume() {
               super.onResume();
       }
       
        /**
        * 暂停fragment
        */
        @Override
        public void onPause() {
               super.onPause();
       }
       
        /**
        * 停止fragment
        */
        @Override
        public void onStop() {
               super.onStop();
       }
       
        /**
        * 销毁fragment所包含的view组件式才会调用
        */
        @Override
        public void onDestroyView() {
               super.onDestroyView();
       }
       
       
        /**
        * 销毁fragment时会被回调
        */
        @Override
        public void onDestroy() {
               super.onDestroy();
       }
       
        /**
        * fragment从activity中删除时会回调该方法,并只会回调一次
        */
        @Override
        public void onDetach() {
               super.onDetach();
       }
}
stareblankly.cn
原文地址:https://www.cnblogs.com/stareblankly/p/4831862.html