一.概述
今天来介绍fragment+PageSlider的使用,在这之前已经介绍了鸿蒙Fraction嵌套使用,我们已经实现了fragment,fragment可用来代替Fraction来使用。
项目地址:https://gitee.com/a3077932030/hmwidget_hmos
二.实现FragmentPagerProvider
FragmentPagerProvider需要继承PageSliderProvider,才能被PageSlider添加。
public abstract class FragmentPagerProvider extends PageSliderProvider {
protected FragmentManager fragmentManager;
public FragmentPagerProvider(FragmentManager fragmentManager) {
this.fragmentManager = fragmentManager;
}
public abstract Fragment getFragment(int i);
@Override
public Object createPageInContainer(ComponentContainer componentContainer, int i) {
Fragment fg = getFragment(i);
fragmentManager.beginTransaction().attach(fg).commitNow();
Component component = fg.getComponent();
componentContainer.addComponent(component);
return component;
}
@Override
public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
componentContainer.removeComponent((Component) o);
destroyComponent(componentContainer, i, o);
}
@Override
public boolean isPageMatchToObject(Component component, Object o) {
return component == o;
}
protected void destroyComponent(ComponentContainer componentContainer, int i, Object o) {
fragmentManager.beginTransaction().remove(getFragment(i)).commit();
}
}
三.fragment+PageSlider的使用
1.首先实现fragment
导入依赖
implementation 'io.gitee.a3077932030:hmwidget:1.0.1'
先弄个base类方便添加fragment布局
public abstract class BaseFragment extends Fragment {
@Override
protected Component onCreateComponent(LayoutScatter scatter, ComponentContainer container) {
return scatter.parse(getXmlId(), container, false);
}
protected abstract int getXmlId();
}
随便弄几个fragment。
public class ImgFragment extends BaseFragment {
@Override
protected int getXmlId() {
return ResourceTable.Layout_fg_img;
}
Image image;
@Override
protected void initComponent() {
image = findComponentById(ResourceTable.Id_image);
}
}
fragment布局代码
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:background_element="#fff"
ohos:orientation="vertical">
<Image
ohos:id="$+id:image"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:image_src="$media:cizi"
/>
</DirectionalLayout>
2.再实现PageSlider的代码
MainAbilitySlice布局代码:
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<PageSlider
ohos:id="$+id:page_slider"
ohos:height="match_parent"
ohos:width="match_parent"
/>
</DirectionalLayout>
MainAbilitySlice代码实现
public class MainAbilitySlice extends FragmentAbilitySlice {
PageSlider pageSlider;
Fragment imgFg, listFg, videoFg;
List<Fragment> fragmentList;
FragmentPagerProviderTest pageSliderProvider;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
fragmentList = new ArrayList<>();
imgFg = new ImgFragment();
listFg = new ImgFragment();
videoFg = new ImgFragment();
fragmentList.add(imgFg);
fragmentList.add(listFg);
fragmentList.add(videoFg);
pageSliderProvider = new FragmentPagerProvider(getFragmentManager()){
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public Fragment getFragment(int i) {
return fragmentList.get(i);
}
};
pageSlider.setProvider(pageSliderProvider);
}
}
MainAbilitySlice 需要继承 FragmentAbilitySlice类。
如果Fraction需要继承FragmentFraction。
FragmentFraction和FragmentAbilitySlice在new FragmentPagerProvider(FragmentManager) 的时候需要传入FragmentManager,只需要调用getFragmentManager()。而fragment调用是getChildFragmentManager()方法。