《第一行代码》学习笔记——第四章 探究碎片
碎片Fragment是一种可以嵌入在活动中的UI片段。它能让程序更加合理和充分地利用大屏幕的空间。
碎片的使用方式
简单使用
- 新建left_fragment.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/>
</LinearLayout>
- 新建right_fragment.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:text="This is right fragment"/>
</LinearLayout>
- 新建LeftFragment和RightFragment类:
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
- 在xml中使用Fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/left_fragment"
android:name="com.lhh.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/right_fragment"
android:name="com.lhh.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
动态添加碎片
示例代码:
Fragment fragment = new RightFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit();
- 新建待添加的Fragment实例
- 获取FragmentManager在活动中可以通过getSupportFragmentManager()方法获得
- 通过调用beginTransaction()方法开启一个事务
- 向容器内添加或替换碎片,一般使用replace()方法实现,需传入容器id和待添加的碎片实例
- 通过调用commit()方法提交事务
在碎片中模拟返回栈
transaction.addToBackStack(null);
碎片和活动之间进行通信
为了方便碎片和活动之间进行通信,FragmentManager提供了findFragmentById()方法用于从布局文件中获取碎片实例:
RightFragment fragment = (RightFragment)getSupportFragmentManager().findFragmentById(R.id.right_fragment);
碎片可以通过调用getActivity()方法或的和当前碎片实例关联的活动实例:
MainActivity activity = (MainActivity)getActivity();
碎片的生命周期
碎片的状态
状态 | 特点 |
---|---|
运行状态 | 碎片可见且关联的活动处于运行状态 |
暂停状态 | 关联的活动进入暂停状态 |
停止状态 | 关联的活动进入停止状态或碎片被移除,替换且之前被加入返回栈中 |
销毁状态 | 关联的活动被销毁或碎片被移除,替换且之前未被加入返回栈中 |
碎片的生命周期回调
活动中有的回调方法,碎片中几乎都有。碎片还提供了一些附加的回调方法:
- onAttach():当碎片和活动建立关联的时候调用
- onCreateView():为碎片创建视图(加载布局)时调用
- onActivityCreated():确保与碎片关联的活动一定已经创建完毕的时候调用
- onDestroyView():当与碎片关联的视图被移除的时候调用
- onDetach():当碎片和活动解除关联的时候调用
动态加载布局技巧
使用限定符
修改activity_main.xml,仅保留左侧碎片,并充满父布局;在res目录下新建layout-large文件夹,在其中新建activity_main.xml,让其包含左右两个碎片。设备会根据屏幕尺寸自动选择要加载的布局。
限定符 | 描述 |
---|---|
small | 提供给小屏幕设备的资源 |
normal | 提供给中等屏幕设备的资源 |
large | 提供给大屏幕设备的资源 |
xlarge | 提供给超大屏幕设备的资源 |
ldpi | 提供给低分辨率设备的资源(<120dpi) |
mdpi | 提供给中等分辨率设备的资源(120dpi~160dpi) |
hdpi | 提供给高分辨率设备的资源(160dpi~240dpi) |
xhdpi | 提供给超高分辨率设备的资源(240dpi~320dpi) |
xxhdpi | 提供给超超高分辨率设备的资源(320dpi~480dpi) |
land | 提供给横屏设备的资源 |
port | 提供给竖屏设备的资源 |
使用最小宽度限定符
最小宽度限定符允许我们对屏幕的宽度指定一个最小值(以dp为单位),以此值为临界点,大于小于分别加载不同的布局。
在res目录下新建layout-sw600dp文件夹,则当程序运行在屏幕宽度大于等于600dp的设备上时会加载layout-sw600dp里的布局文件,小于时加载默认的layout里的布局文件