短视频程序源码,PageSlider实现滑动页面
短视频程序源码,PageSlider实现滑动页面的相关代码
TestPagerProvider.java
package com.testruiwang.myapplication3.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
@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("Page A"));
dataItems.add(new TestPagerProvider.DataItem("Page B"));
dataItems.add(new TestPagerProvider.DataItem("Page C"));
dataItems.add(new TestPagerProvider.DataItem("Page D"));
return dataItems;
}
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:alignment="center"
ohos:orientation="vertical">
<PageSlider
ohos:id="$+id:page_slider"
ohos:height="300vp"
ohos:width="300vp"
ohos:layout_alignment="horizontal_center"/>
</DirectionalLayout>
以上就是 短视频程序源码,PageSlider实现滑动页面的相关代码,更多内容欢迎关注之后的文章