安卓学习之—Fragment

Fragment 基础

有点类似于Activity,有自己的布局,有自己的生命周期,可以处理用户事件,由于FragmentActivity具有相似性,而且能在Activity中进行灵活切换,现在fragment大量的应用在手机平板间。

Fragment 版本

由于Fragment是在 3.0后才有的,要使用 Fragment SDK版本需要大于 11;由于Fragment的广泛使用,google后期在V4包中提供了 Fragment的支持.在实际开发过程中,V4包中Fragment得到广泛使用

Fragment 的创建

1.      新建Class继承 android.app.Fragment(sdk >= 11) android.support.v4.app.Fragment(通用,建议使用)

2.      重写 onCreateView 方法:

3.   @Override

4.   public View onCreateView(LayoutInflaterinflater, ViewGroup container,

5.       Bundle savedInstanceState) {

6.       /**

7.        *为此fragment 加载你定义的布局文件

8.       */

9.       returninflater.inflate(R.layout.your_fragment_layout, container, false);

10.  }

注意: inflater.inflate(R.layout.your_fragment_layout,container, false); 中的参数必须为 false.

Fragment 的生命周期

1.      onAttach()

fragment 宿主Activity关联时调用。

2.      onCreate()

在创建fragment时系统会调用此方法。

3.      onCreateView()

fragment 的视图挂载到到宿主Activity的视图上时调用。

4.      onActivityCreated()

宿主Activity onCreate()方法执行完成返回后调用。

5.      onStart()

fragment 相对于用户而言,往可见方向的过程中,此时用户可见但不可操作时调用。

6.      onResume()

fragment 相对于用户而言,往可见方向的过程中,此时用户可见也可操作时调用。

7.      onPause()

fragment 相对于用户而言,往不可见方向的过程中,此时用户可见但不可操作时调用。

8.      onStop()

fragment 相对于用户而言,往不可见方向的过程中,此时用户不可见也不可操作时调用。

9.      onDestroyView()

fragment 的视图从到宿主Activity的视图中移除时调用。

10.     onDestroy()

fragment 销毁时调用。

11.     onDetach()

fragment 宿主Activity解除关联时调用。

Fragment Activity间的关系

·         Activity类似,Fragment的设计是用来提供用户交互接口的;

·        但是Fragment不能单独存在,必须寄宿在Activity中;

·        一个Activity实例可以寄宿多个Fragment实例;

·        一个Fragment实例只能有一个 Activity实例作为宿主;

·        Activity通过 FragmentManager来管理Fragment;

Fragment 的加载方式

XML配置加载(fragment的类型必须指定,因此通用性,灵活性不强。

·        配置的步骤

1.     创建 Activity

MainActivity.java

``` public class MainActivity extendsFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

} ```

2.     创建 Fragment

LeftFragment.java

public class LeftFragment extends Fragment { @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,BundlesavedInstanceState) { /** * Inflate the layout for this fragment */ returninflater.inflate(R.layout.left_fragment, container, false); } }RightFragment.java

public class RightFragment extends Fragment { @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){ /** * Inflate the layout for this fragment */ returninflater.inflate(R.layout.right_fragment, container, false); } }

3.     创建 FragmentXML布局

<fragment

        android:id="@+id/fragment_my"

       android:layout_width="match_parent"

        android:layout_height="match_parent"

      class="com.example.fragmentwithxml.MyFragment"/>

动态加载
FragmentManager

·        作用

Fragment 添加到Activity视图中来,或是从Activity中移除。

·        如何拿到FragmentMannager

FragmentManager的作用可以了解到,FragmentManager Activity的一个工具,用来添加和移除 Fragment工具,因此 FragmentManager Activity中用的,也是在Activity中获得的。

SDK >= 11

FragmentManager fm = getFragmentManager();

通用V4:(Activity必须继承 FragmentActivity)

FragmentManager fm = getSupportFragmentManager();

 

Fragment 的常用方法

// 得到Fragment管理者

FragmentManager fragmentManager =getSupportFragmentManager();

// 开启事物

FragmentTransaction transaction =fragmentManager.beginTransaction();

//参数1参数2 :指的是你切换的时候的,进入和退出的动画

//参数3参数4  :指的是按返回键的时候的进入的退出的动画

transaction.setCustomAnimations(R.anim.next_enter,R.anim.next_exit, R.anim.pre_enter, R.anim.pre_exit);(动画在添加和替换Fragment之前设置

// Add操作,每次都会让容器中添加一个新的Fragment对象

tx.add(R.id.left_container, newMyFragment()); //添加一个Fragment(在一个事物里,相同对象只能添加一次)

// int : 要装替换的Fragment对象的容器的id

// Fragment : 要替换的Fragment对象

// replace操作:替换操作,在装入要替换的Fragment之前会吧之前的Fragment清除

tx.replace(R.id.right_container, newMyFragment());

add:加入的Fragment会长期驻于内存中,消耗内存,但数据如果无需改变且界面经常使用,推荐使用此方式
replace:加入的Fragment会替换之前的Fragment,不占用内存,如果数据经常改变,推荐使用此种方式

 

Fragment fragmentA = new FragmentA();

fragmentA.isAdded()//判断 fragmentA是否已经添加过

fragmentA.isVisible() //判断fargmentA是否是显示

transaction.show(fragmentA); //显示一个fragment

transaction.hide(fragmentA); //隐藏一个Fragment

transaction.addToBackStack(null);//将事物添加到回退栈,按返回键是fragment也要一个个返回

transaction.commit();//提交事物,一个事物只能提交一次;

 

Fragment 的通信
1,ActivityFragment通信
MyFragment myFragment = new MyFragment();
Bundle args=new Bundle();
args.putString("msg", "这是activity给你的");
//核心代码:Fragment身上设置一个参数
myFragment.setArguments(args);
Fragment:将数据取出
Bundle arguments = getArguments();
String msg = arguments.getString("msg");
2,FragmentActivity通信
//获取与当前Fragment绑定的Activity的引用,强转为绑定Activity,调用方法,实现传值,类似回调的方式
MainActivity activity = (MainActivity) getActivity();
activity.receiveMsg("这是fragment给你的");

3,FragmentFragment进行通信
官方建议用接口回调

Envent Bus框架使用(导入jar包)

1.第一步写一个类创造一条总线对象

public class BusFactory {

   //创造一条总线对象

   private static Bus bus=new Bus();

   private BusFactory(){

     

   }

   public static  Bus getBus(){

      return bus;

   }

}

2.注册总线

BusFactory.getBus().register(this);

3.接到消息的回调方法

@Subscribe  //编写当接到消息后,用于处理消息的方法,标识必须写,方法可以自定义

   public void receiveMsg(Object data){

      //获取到MainActivity传过来的数据,并显示

      if (isChange) {

        isChange=false;

        return;

      }

      et.setText(data.toString());

   }

4.发送消息

BusFactory.getBus().post(data);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值