Android Fragment

碎片(Fragment)

碎片是一种可以嵌入在活动中的UI碎片,可以理解为一个迷你型的活动,通常在平板开发中使用

动态添加/替换碎片
  • 创建待添加的碎片实例
  • 获取FragmentManager,在活动中通过getSupportFragmentManager()获得
  • 开始一个事务,调用beginTransaction()开启
  • 向容器添加add或替换replace()碎片
  • 提交事务commit()
1
2
3
4
5
6
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager=getSupportFragmentManager(); //获取FragmentManager实例
FragmentTransaction transaction=fragmentManager.beginTransaction(); //开启一个事务
transaction.replace(R.id.right_layout,fragment);
transaction.commit(); //提交事务
}
在碎片中模拟返回栈

上面的实例中,我们点击按钮替换了新的碎片后,按下BACK键会直接退出,如何返回到上一个碎片?

addToBackStack()方法可以将一个事务添加到返回栈当中

1
2
3
4
5
6
7
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
transaction.addToBackStack(null); //添加到返回栈 接收一个名字用于描述返回栈的状态,一般传入null即可
transaction.commit();
}
碎片和活动之间的通信
  • 为了方便碎片和活动之间进行通信,FragmentManager提供了一个findFragmentById()方法,用于从布局文件中获取碎片的实例
1
RightFragMent rightFragment = (RightFragment) getSupportFragmentManger().findFragmentById(R.id.right_fragment);
  • 碎片中调用活动 getActivity() 方法来获取相关的活动实例
1
MainActivity activity = (MainActivity) getActivity();
  • 另外,在碎片中需要使用Context对象时,也可以使用getActivity()方法,因为获取到的活动本身就是一个Context对象
碎片的声明周期
四种状态(碎片依附于活动)
  • 运行态:碎片可见,所关联的活动处于运行态
  • 暂停态:所关联的活动处于暂停态
  • 停止态:所关联的活动处于停止态,或通过replace()和remove()等方法移除掉碎片。总之,碎片此时完全不可见,有可能被系统回收
  • 销毁态:所关联的活动处于销毁态,或通过replace()和remove()等方法移除掉碎片。总之,碎片此时完全不可见,已经被系统回收
五个回调方法
  • onAttach(): 当碎片和活动建立关联时调用
  • onCreateView(): 为碎片加载布局时调用
  • onActivityCreated(): 确保与碎片相关联的活动一定已经创建完毕的时候调用
  • onDestroy(): 当与碎片关联的视图被移除的时候调用
  • onDetach(): 当碎片和活动解除关联的时候调用

动态加载布局
使用限定符
  • 主布局,只留下左侧碎片并充满整个父布局 activity_main.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.example.day06_fragment.LeftFragment"
    android:id="@+id/left_fragment"
    />
    </LinearLayout>
  • 大布局

    在res新建一个layout-large文件夹,在里面新建一个activity_main.xml布局,其中large就是一个限定符,屏幕被认为是large的设备会自动加载对应文件夹下的布局

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:name="com.example.day06_fragment.LeftFragment"
    android:id="@+id/left_fragment"
    android:layout_weight="1"/>

    <fragment
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:name="com.example.day06_fragment.RightFragment"
    android:id="@+id/right_fragment"
    android:layout_weight="3"/>

    </LinearLayout>
  • 运行效果:

常见限定符
  • 大小:small,normal,large,xlarg
  • 分辨率:ladpi(低分辨率),mdpi,hdpi,xhdpi,xxhdpi(超超高分辨率)
  • 方向:land(横屏),port(竖屏)
最小宽度限定符
  • Smallest-width Qualifier允许我们对屏幕的宽度限定一个最小值,以这个最小值为临界点,大于这个值解加载一个布局,屏幕宽度小于这个值的设备就加载另一个布局
  • 新建布局:在res下新建一个layout-sw600dp文件夹,在里面新建一个activity_main.xml布局,这就意味着程序在屏幕宽度小于600dp的设备是layout/activity_main.xml布局,大于等于600dp用layout-sw600dp/activity_main.xml布局
0%