零基础学鸿蒙编程-UI控件_PageSlider+PageSliderIndicator 原创

蓝不蓝编程
发布于 2021-11-26 22:14
浏览
0收藏

什么是PageSlider、PageSliderIndicator

PageSlider是用于实现整页切换.
PageSliderIndicator用于辅助显示当前所处页面。
效果图:
零基础学鸿蒙编程-UI控件_PageSlider+PageSliderIndicator-鸿蒙开发者社区

基础用法

  1. 在主布局文件"ability_main.xml"中增加PageSliderPageSliderIndicator
<StackLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent">

    <PageSlider
        ohos:id="$+id:pager_slider"
        ohos:height="match_parent"
        ohos:width="match_parent"/>

    <PageSliderIndicator
        ohos:id="$+id:page_slider_indicator"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:item_offset="60vp"
        ohos:layout_alignment="bottom|horizontal_center"
        ohos:padding="8vp"/>
</StackLayout>
  1. 在Slice中实例化PageSlider,设置数据
public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_pager_slider);
        LayoutScatter layoutScatter = LayoutScatter.getInstance(getContext());
        Component component1 = layoutScatter.parse(ResourceTable.Layout_page1, null, false);
        Component component2 = layoutScatter.parse(ResourceTable.Layout_page2, null, false);

        List<Component> components = new ArrayList<>();
        components.add(component1);
        components.add(component2);
        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return components.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                componentContainer.addComponent(components.get(i));
                return components.get(i);
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent(components.get(i));
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return component == o;
            }
        });

        PageSliderIndicator pageSliderIndicator = (PageSliderIndicator) findComponentById(ResourceTable.Id_page_slider_indicator);
        pageSliderIndicator.setPageSlider(pageSlider);
    }
}
  1. 两个页面对应代码:
  • page1.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent">

    <Text
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:text="PageSlider1"
        ohos:text_alignment="center"
        ohos:text_size="25fp"/>
</DirectionalLayout>
  • page2.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent">

    <Text
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:text="PageSlider2"
        ohos:text_alignment="center"
        ohos:text_size="25fp"/>
</DirectionalLayout>

完整源代码

https://gitee.com/hspbc/harmonyos_demos/tree/master/pageSliderDemo

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
1
收藏
回复
举报
回复
    相关推荐