自学鸿蒙应用开发(37)- PageSlider组件

2021-04-22 12:07:30 浏览数 (1)

PageSlider是用于页面之间切换的组件,它通过响应滑动事件完成页面间的切换。

效果展示

首先是本文所示代码的动作视频: http://mpvideo.qpic.cn/0b78eyaakaaaeyapga3k7nqfajwdautaabia.f10003.mp4?dis_k=2f0fd368b9f1d417051633a1ef530023&dis_t=1619063338&spec_id=MzI2MDYyNDgzMQ==1619063421&vid=wxv_1812446965637414914&format_id=10003

准备布局文件

一共用到三个布局,第一布局是控制画面全体的page_slider_owner.xml,其内部定义了一个PageSlider组件:

代码语言:javascript复制
<?xml version="1.0" encoding="utf-8"?>
<PageSlider
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:id="$ id:page_slider"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:layout_alignment="center"/>

接下来是用于控制第1页,第3页的布局文件1:

代码语言:javascript复制
<?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"
    ohos:layout_alignment="center"
    ohos:background_element="#7FFF7F">

    <Text
        ohos:id="$ id:text_helloworld"
        ohos:height="match_parent"
        ohos:width="match_content"
        ohos:layout_alignment="center"
        ohos:text_color="#0000FF"
        ohos:text_size="80"
        />
</DirectionalLayout>

用于控制第2页,第4页的布局文件2:

代码语言:javascript复制
<?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"
    ohos:background_element="#FFFF7F">

    <Text
        ohos:id="$ id:text_helloworld"
        ohos:height="match_content"
        ohos:width="match_content"
        ohos:layout_alignment="horizontal_center"
        ohos:text_color="#FF0000"
        ohos:text_size="200"
        />
</DirectionalLayout>

布局结构基本相同,区别仅仅在于背景颜色,字体大小和字体颜色。

定制PageSliderProvider

PageSliderProvider继承了PageSliderProcer类,为PageSlider定义了页面生成/销毁功能。

代码语言:javascript复制
public class TestPagerProvider extends PageSliderProvider {
    private List<DataItem> list;
    private AbilitySlice slice;
    public TestPagerProvider(List<DataItem> list, AbilitySlice slice){
        this.list = list;
        this.slice = slice;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        final DataItem data = list.get(i);

        Component cpt = LayoutScatter.getInstance(slice).parse(data.mLayout, null, true);
        Text label = (Text)cpt.findComponentById(ResourceTable.Id_text_helloworld);
        label.setText(data.mText);
        componentContainer.addComponent(cpt);
        return cpt;
    }

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

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

    //数据实体类
    public static class DataItem{
        String mText;
        int mLayout;
        public DataItem(String txt, int id) {
            mText = txt;
            mLayout = id;
        }
    }
}

内嵌的数据类DataItem,为页面提供布局id和文字信息。实际开发时PageSliderProvider 的派生类和DataItem应该是设计重点。

导入PageSlider

代码语言:javascript复制
public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_page_slider_owner);
        initPageSlider();
    }
    private void initPageSlider() {
        PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
        pageSlider.setProvider(new TestPagerProvider(getData(), this));
    }
    private ArrayList<TestPagerProvider.DataItem> getData() {
        ArrayList<TestPagerProvider.DataItem> dataItems = new ArrayList<>();
        dataItems.add(new TestPagerProvider.DataItem("Page A", ResourceTable.Layout_ability_main1));
        dataItems.add(new TestPagerProvider.DataItem("Page B", ResourceTable.Layout_ability_main2));
        dataItems.add(new TestPagerProvider.DataItem("Page C", ResourceTable.Layout_ability_main1));
        dataItems.add(new TestPagerProvider.DataItem("Page D", ResourceTable.Layout_ability_main2));
        return dataItems;
    }
}

initPageSlider用于导入自定义的TestPagerProvier类,getData用于生成每个页面的DataItem数据。

参考代码

完整代码可以从以下链接下载:

https://github.com/xueweiguo/Harmony/tree/master/PageSlider

0 人点赞