HarmonyOS 应用开发 - PageSlider体验与分享 原创
PageSlider是用于页面之间切换的组件,它通过响应滑动事件完成页面间的切换。
ability_main.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"
ohos:orientation="vertical">
<PageSlider
ohos:id="$+id:page_slider"
ohos:height="300vp"
ohos:width="300vp"
ohos:layout_alignment="horizontal_center"/>
</DirectionalLayout>
TestPagerProvider.java代码:
package com.example.pageslider.slice;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import java.util.List;
public class TestPagerProvider extends PageSliderProvider {
// 数据源,每个页面对应list中的一项
private List<DataItem> list;
public TestPagerProvider(List<DataItem> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object createPageInContainer(ComponentContainer componentContainer, int i) {
final DataItem data = list.get(i);
Text label = new Text(null);
label.setTextAlignment(TextAlignment.CENTER);
label.setLayoutConfig(
new StackLayout.LayoutConfig(
ComponentContainer.LayoutConfig.MATCH_PARENT,
ComponentContainer.LayoutConfig.MATCH_PARENT
));
label.setText(data.mText);
label.setTextColor(Color.BLACK);
label.setTextSize(50);
ShapeElement element = new ShapeElement();
element.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor("#AFEEEE")));
label.setBackground(element);
componentContainer.addComponent(label);
return label;
}
@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;
public DataItem(String txt) {
mText = txt;
}
}
}
MainAbilitySlice.java代码:
package com.example.pageslider.slice;
import com.example.pageslider.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;
import java.util.ArrayList;
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
initPageSlider();
}
private void initPageSlider() {
PageSlider pageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);
pageSlider.setProvider(new TestPagerProvider(getData()));
}
private ArrayList<TestPagerProvider.DataItem> getData() {
ArrayList<TestPagerProvider.DataItem> dataItems = new ArrayList<>();
dataItems.add(new TestPagerProvider.DataItem("第一个页面"));
dataItems.add(new TestPagerProvider.DataItem("第二个页面"));
dataItems.add(new TestPagerProvider.DataItem("第三个页面"));
dataItems.add(new TestPagerProvider.DataItem("第四个页面"));
return dataItems;
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
完整代码地址:
https://gitee.com/jltfcloudcn/jump_to/tree/master/jltf_PageSlider_component
老师的视频链接我搬一下:https://harmonyos.51cto.com/show/3762
谢谢,本来应该是一体的。
视频嵌入文章的功能敬请期待。