HarmonyOS 应用开发-ListContainer体验与分享 原创
ListContainer是用来呈现连续、多行数据的组件,包含一系列相同类型的列表项。下面通过演示显示效果。
显示效果:
点击图中的内容会有提示
可以上下滑动
SampleItemProvider.java中的代码
package com.example.jltftiyan9.slice;
import com.example.jltftiyan9.ResourceTable;
import com.example.jltftiyan9.SampleItem;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import java.util.List;
public class SampleItemProvider extends BaseItemProvider {
private List<SampleItem> list;
private AbilitySlice slice;
public SampleItemProvider(List<SampleItem> list, AbilitySlice slice) {
this.list = list;
this.slice = slice;
}
@Override
public int getCount() {
return list == null ? 0 : list.size();
}
@Override
public Object getItem(int position) {
if (list != null && position >= 0 && position < list.size()){
return list.get(position);
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
final Component cpt;
if (convertComponent == null) {
cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_jltfitem_sample, null, false);
} else {
cpt = convertComponent;
}
SampleItem sampleItem = list.get(position);
Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_index);
text.setText(sampleItem.getName());
return cpt;
}
}
完整代码地址:
https://gitee.com/jltfcloudcn/jump_to/tree/master/jltf_ListContainer_component