Fragment介绍

Fragment 是什么

碎片(Fragment)是一种可以嵌入在活动(activity)当中的 UI 片段。

碎片的简单用法

创建两个布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Button"
            />
    </LinearLayout>

//left_fragment.xml
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:orientation="vertical" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textSize="20sp"
            android:text="This is right fragment"
            />
    </LinearLayout>

//right_fragment.xml

所有的自定义 Fragment 都需要继承 Fragment 类:

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

最后定义 activity_main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <fragment
            android:id="@+id/left_fragment"
            android:name="com.example.fragmenttest.LeftFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <fragment
            android:id="@+id/right_fragment"
            android:name="com.example.fragmenttest.RightFragment"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

动态添加碎片

可以在代码当中动态添加碎片:

@Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button:
                AnotherRightFragment fragment = new AnotherRightFragment();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction transaction = fragmentManager.
    beginTransaction();
                transaction.replace(R.id.right_layout, fragment);
                transaction.commit();
                break;
            default:
                break;
    } }

动态添加碎片主要分为 5 步。

  1. 创建待添加的碎片实例。
  2. 获取到 FragmentManager,在活动中可以直接调用 getFragmentManager()方法得到。
  3. 开启一个事务,通过调用 beginTransaction()方法开启。
  4. 向容器内加入碎片,一般使用 replace()方法实现,需要传入容器的 id 和待添加的碎片实例。
  5. 提交事务,调用 commit()方法来完成。

在碎片中模拟返回栈

通过点击按钮添加了一个碎片之后,这时按下 Back 键程序就会直接退出。

 public class MainActivity extends Activity implements OnClickListener {
......
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button:
                AnotherRightFragment fragment = new AnotherRightFragment();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction transaction = fragmentManager.
    beginTransaction();
                transaction.replace(R.id.right_layout, fragment);
                transaction.addToBackStack(null);
                transaction.commit();
                break;
            default:
    break; }
    }
}

碎片和活动之间进行通信

为了方便碎片和活动之间进行通信,FragmentManager 提供了一个类似于 findViewById() 的方法,专门用于从布局文件中获取碎片的实例,代码如下所示:

    RightFragment rightFragment = (RightFragment) getFragmentManager()
            .findFragmentById(R.id.right_fragment);

在每个碎片中都可以通过调用 getActivity() 方法来得到和当前碎片相关联 的活动实例,代码如下所示:

    MainActivity activity = (MainActivity) getActivity();

碎片的生命周期

  1. 运行状态:当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态。
  2. 暂停状态:当一个活动进入暂停状态时(由于另一个未占满屏幕的活动被添加到了栈顶),与它相关联的可见碎片就会进入到暂停状态。
  3. 停止状态:当一个活动进入停止状态时,与它相关联的碎片就会进入到停止状态。
  4. 销毁状态:碎片总是依附于活动而存在的,因此当活动被销毁时,与它相关联的碎片就会进入 到销毁状态。

下面是碎片的一些回调方法:

  1. onAttach()当碎片和活动建立关联的时候调用。
  2. onCreateView()为碎片创建视图(加载布局)时调用。
  3. onActivityCreated()确保与碎片相关联的活动一定已经创建完毕的时候调用。
  4. onDestroyView()当与碎片关联的视图被移除的时候调用。
  5. onDetach()当碎片和活动解除关联的时候调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值