赞
踩
安卓有fragment组件,那鸿蒙有没有类似的组件呢?其实鸿蒙早就提供了类似于fragment的组件,那就是Fraction。这次就向大家介绍Fraction的使用以及Fraction的生命周期
- public class TestFractionAbility extends FractionAbility {
-
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setMainRoute(TestFractionAbilitySlice.class.getName());
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:orientation="vertical"
- ohos:background_element="$graphic:background_ability_main"
- ohos:width="match_parent">
-
- <StackLayout
- ohos:id="$+id:test_fraction"
- ohos:width="match_parent"
- ohos:height="match_parent"/>
-
- </DirectionalLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:orientation="vertical"
- ohos:background_element="$graphic:background_ability_main"
- ohos:width="match_parent">
-
- <Text
- ohos:background_element="$graphic:background_ability_main"
- ohos:height="match_content"
- ohos:layout_alignment="horizontal_center"
- ohos:text="home fraction"
- ohos:text_size="50"
- ohos:width="match_content"
- />
-
- </DirectionalLayout>

- public class TestFraction extends Fraction {
- @Override
- protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
- Component component = scatter.parse(ResourceTable.Layout_test_fraction, container, false);
- return component;
- }
-
- @Override
- protected void onStart(Intent intent) {
- super.onStart(intent);
-
- }
- }
- public class TestFractionAbilitySlice extends AbilitySlice {
-
- @Override
- protected void onStart(Intent intent) {
- super.onStart(intent);
- setUIContent(ResourceTable.Layout_ability_test_fraction);
- ((FractionAbility)getAbility()).getFractionManager()
- .startFractionScheduler()
- .add(ResourceTable.Id_test_fraction, new TestFraction())
- .submit();
- }
- }
代码运行后:
Fraction生命周期主要涉及到八个生命周期方法,分别是onComponentAttached、onStart、onActive、onInactive、onBackground、onForeground、onStop、onComponentDetach
在实际开发中,我们可能需要让Fraction嵌套Fraction,目前鸿蒙还不支持Fraction嵌套Fraction。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。