Fragment

Android3.0新增的Fragment类,特性:

1、程序的运行界面可以由多个Fragment组成;

2、每个Fragment都有各自独立的运行状态,并且接受各自的处理事件;

3、在程序运行的过程中,Fragment可以动态地加入和移除。

使用Fragment的步骤:

1、新增一个继承Fragment的新类;

2、加入需要处理的状态转换方法:

onCreate():当Fragment刚被建立时运行,可以在其中完成变量的初始设置;

onCreateView():当Fragment将要显示在屏幕上时运行,必须在其中设置接口

onPause():当Fragment要从屏幕中消失时运行,可以在其中存储用户操作状态和资料,以便下次Fragment重新显示时继续之前的工作。

public class Fragment1 extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment1, container,false);
            return view;
            }
}

3、在主程序布局文件中加入<fragment>标签:

<FrameLayout android:layout_weight="1"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:background="?android:attr/detailsElementBackground"
>
<fragment android:id="@+id/fra"
     android:name="com.fragment.fragment1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"    
>
</fragment>
</FrameLayout>

注意:

1、fragment的开头字母必须是小写;

2、每个fragment标签都必须设置id属性;

3、fragment中的name属性是指定所使用的Fragment类,且必须加上完整的组件路径名称;

4、使用layout_weight属性时,以设置比例方式控制宽度,此时layout_width必须设置为0dp;

 

FragmentManager对象提供三个控制Fragment的方法:

1、add():把Fragment加入程序的操作接口

2、remove():从程序的操作接口中移除指定的Fragment

3、replace():用新的Fragment取代程序界面中的Fragment

//取得FragmentManager对象
FragmentManager fragMgr = getFragmentManager();
//建立FragmentTransaction对象记录操作
FragmentTransaction fragTran = fragMgr.beginTransaction();
Fragment1 frag1 = new Fragment1();
Fragment2 frag2 = new Fragment2();
//使用FragmentTransaction控制Fragment
/*
 *第一个参数为指定要将Fragment加入哪一个接口组件中
 *第二个参数为Fragment对象
 *第三个参数为Tag
*/
fragTran.add(R.id.framelayout, frag1 ,"My Fragment1");
fragTran.replace(R.id.framelayout, frag2 ,"My Fragment2");
Fragment2 frag_2 = (Fragment2)fragMgr.findFragmentByTag("My Fragment2");
fragTran.remove(frag_2);
//更新处理流程
fraTran.commit();
//也可用匿名对象简化
//replace()第一次使用和add()结果一样
//getFragmentManager().beginTransaction().replace(R.id.framelayout, frag2 ,"My Fragment2").commit();

 

Fragment 与 Activity 之间的callback机制

1、在Fragment中定义一个Interface,其中包含需要由Main类提供的callback方法

1 public class Fragment1 extends Fragment {
2     public interface CallbackInterface {
3        public void method1(...);
4        public void method2(...);
5        ...
6 };
7 }

2、在Main类中实现CallbackInterface

public class Main extends Activity implements Fragment1.CallbackInterface {
         public void method1(...) {
          ...
         }
         public void method2(...) {
          ...
         }
}

3、Main类中的callback方法传给Fragment1,可在Fragment1中的onAttach()中完成

 1 public class Fragment1 extends Fragment {
 2      ...
 3      private CallbackInterface mCallback;
 4          @Override
 5     public void onAttach(Activity activity) {
 6         // TODO Auto-generated method stub
 7         super.onAttach(activity);
 8        try {
 9                mCallback = (CallbackInterface)activity;
10     }catch (ClassCastException e){
11            throw new ClassCastException(activity.toString() + "must implement Fragment1.CallbackInterface.");
12     }
13     }

 

posted @ 2015-08-26 20:57  Ghimtim  阅读(179)  评论(0编辑  收藏  举报