Android中Fragmeng的示例代码

1.1    Fragment切换示例代码

<FrameLayout

       android:id="@+id/framelayout_main"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:layout_above="@+id/tv1">

        <!-- 稍后用于添加或替换显示Fragment-->

</FrameLayout>

    <TextView

       android:id="@+id/tv1"

       android:layout_width="match_parent"

       android:layout_height="2dp"

       android:layout_above="@+id/radioGroup1"

       android:layout_marginBottom="5dp"

       android:background="#000000"/>

    <RadioGroup

       android:id="@+id/radioGroup1"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:layout_alignParentBottom="true"

       android:orientation="horizontal">

<RadioButton

           android:id="@+id/radio0"

           android:layout_width="0dp"

           android:layout_height="wrap_content"

           android:layout_weight="1"

           android:background="@null"

           android:button="@null"

           android:checked="true"

           android:drawableTop="@drawable/slt_btn1_item"

           android:gravity="center"

           android:text="选项一" />。。。。

</RelativeLayout>

1.1.1  Replace形式

public class MainActivity extends Activity {

         FragmentManagermanager;

         RadioGrouprg;

         @Override

         protected voidonCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.activity_main);

                   init();

                   /*

                    * 添加默认显示的Fragment

                    */

                   manager.beginTransaction().add(R.id.framelayout_main,new MyFragment())

                                     .commit();

                   rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                            @Override

                            public voidonCheckedChanged(RadioGroup group, intcheckedId) {

                                     // TODO Auto-generated method stub

                                     switch (checkedId) {

                                     case R.id.radio0:

                                               manager.beginTransaction()

                                                                 .replace(R.id.framelayout_main, newMyFragment())

                                                                 .commit();

                                               break;

                                     case R.id.radio1:

                                               manager.beginTransaction()

                                                                 .replace(R.id.framelayout_main, newTwoFragment())

                                                                 .commit();

                                               break;

                                     case R.id.radio2:

                                               manager.beginTransaction()

                                                                 .replace(R.id.framelayout_main,

                                                                                    new ThreeListFragment()).commit();

                                               break;

                                     }

                            }

                   });

         }

         private voidinit() {

                   // TODO Auto-generated method stub

                   manager = getFragmentManager();

                   rg = (RadioGroup) findViewById(R.id.radioGroup1);

         }

}

拓展:回退栈的运用

// 加入Fragment回退栈的标记

                            transaction.replace(R.id.main,fragment1, "fragment1");

                            transaction.addToBackStack("fragment1");

 

 

1.2    Fragment之间的通讯

Fragment之间的通讯必须通过他们所在的Activity来实现

Fragment.getActivity():获取Fragment所在的Activity对象

Activity.getFragmentManager().findFragmentById(int):在Activity中通过id寻找Fragment

示例:在一个Activity中有两个Fragment碎片,现在实现从两个碎片中相互获取值。

transaction.add(R.id.one, fragment1, "fragment1");

transaction.add(R.id.two, fragment2, "fragment2");

transaction.commit();

碎片一中:

Fragment2 fragment2 = (Fragment2)getFragmentManager().findFragmentByTag("fragment2");

EditText editText = (EditText) fragment2.getView().findViewById(R.id.editText2);

Toast.makeText(getActivity(),"--two->>" +editText.getText().toString(), 1).show();

碎片二中:

Fragment1 fragment1 = (Fragment1) getFragmentManager().findFragmentByTag("fragment1");

EditText editText = (EditText) fragment1.getView().findViewById(R.id.editText1);

Toast.makeText(getActivity(),"--one->>"+ editText.getText().toString(), 1).show();

1.3    Fragment的管理

涉及的类

         FragmentManager

                   每一个Activity都包含一个FragmentManager来管理它所包含的Fragment

                   获取FragmentManager对象:FragmentManagerfm = getFragmentManager();

         FragmentTransaction

                   事务:保证任务执行的完整性

                   FragmentTransaction可以用来在一个Activity内添加、删除和替换Fragment

                   使用步骤:开启并提交事务

                            1、FragmentTransactionft = fm.beginTransaction();

                            2、添加、删除和替换Fragment

                            3、ft.commit();

添加、删除和替换Fragment

         添加Fragment

                   ft.add(int,Fragment)

         删除Fragment

                   ft.remove(Fragment)

         替换Fragment

                   ft.replace(int,Fragment)

使用FragmentManager查找Fragment

         findFragmentById(intid)

         findFragmentByTag(Stringtag)

1.4    Fragment向下兼容

android.support.v4.app.Fragment

android.support.v4.app.FragmentActivity

android.support.v4.app.FragmentManager

         FragmentManagerfm = getSupportFragmentManager();

android.support.v4.app.FragmentTransaction

FragmentManager fm =getSupportFragmentManager();

PS:如果在API11及以上构建的项目上使用了支持Fragment的Android支持包,请确保关于Fragment的相关引入和类引用都来自于Android支持包,虽然Android支持包和Android本地开发包中都含有Fragment的相关类,但这两个分布在不同包中的类是不可以互用的。

要通过Android支持包来使用Fragment,必须要保证Activity是继承自FragmentActivity类

 

1.5    Fragment的生命周期

Fragment的生命周期是依赖于它所在的Activity的生命周期

Fragment生命周期图

         * 生命周期的运行特点:

     * Fragment被添加到页面中进行显示时:

     * 运行流程:

     *onAttach--onCreate--onCreateView--onActivityCreated

     * ---onStart--onResume ,直到运行onResumeFragment才可见可交互

     *

     * Fragment被从页面显示中删除时,

     * 运行流程:

     *onPause--onStop--onDestoryView--onDestroy--onDetach

     *

     * Fragment的生命周期会收Activity的生命周期的影响,即一旦Activity退出,那么

     * Fragment也会自动退出

 

 

1.6    相关类

ListFragment

         定义:带有ListView控件的Fragment

         setListAdapter(BaseAdapter):设置自带的ListView的适配器

         重写onListItemClick()方法处理LIstView的item点击事件

DialogFragment

         定义:带有生命周期的对话框

         创建类,并继承DialogFragment

         重写onCreateView方法,并通过setStyle()设置对话框样式

         重写onCreateDialog()方法,并返回Dialog

         在Activity中通过自定义类的对象调用show(FragmentManagerfm, String tag)

         二选其一

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值