【码云】Gitee仓库地址:https://gitee.com/JIuyang2284/ListContainerTest
Layout
1.布局文件:ability_main
<?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">
<ListContainer
ohos:id="$+id:lc_main"
ohos:height="match_parent"
ohos:width="match_parent"></ListContainer>
</DirectionalLayout>
2.布局文件:item_layout
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:orientation="vertical"
ohos:width="match_parent"
ohos:height="100vp"
ohos:top_margin="3vp">
<DirectionalLayout
ohos:weight="1"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="horizontal">
<DirectionalLayout
ohos:weight="1"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="vertical">
<DirectionalLayout
ohos:width="match_parent"
ohos:height="70vp"
ohos:orientation="horizontal">
<Text
ohos:id="$+id:text_title"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:text_alignment="vertical_center"
ohos:left_padding="10vp"
ohos:right_padding="10vp"
ohos:max_text_lines="2"
ohos:multiple_lines="true"
ohos:text="标题"
ohos:text_color="black"
ohos:text_size="20fp"></Text>
</DirectionalLayout>
<DirectionalLayout
ohos:width="match_parent"
ohos:height="match_parent"
ohos:orientation="horizontal">
<Text
ohos:id="$+id:text_author"
ohos:width="match_content"
ohos:height="match_parent"
ohos:text="新闻来源"
ohos:text_size="13fp"
ohos:text_alignment="center|left"
ohos:left_padding="10vp"></Text>
<Text
ohos:id="$+id:text_date"
ohos:weight="1"
ohos:width="match_parent"
ohos:height="match_parent"
ohos:text="时间"
ohos:text_size="13fp"
ohos:text_alignment="vertical_center|right"
ohos:right_padding="10vp"></Text>
</DirectionalLayout>
</DirectionalLayout>
<Image
ohos:id="$+id:image"
ohos:width="120vp"
ohos:height="80vp"
ohos:scale_mode="zoom_center"
ohos:layout_alignment="center"
ohos:image_src="$media:icon"></Image>
</DirectionalLayout>
<Component
ohos:height="1vp"
ohos:width="match_parent"
ohos:background_element="black"></Component>
</DirectionalLayout>
Java
1.实体类
public class TestClass {
String title;
String author;
String date;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public TestClass(String title, String author, String date) {
this.title = title;
this.author = author;
this.date = date;
}
}
2.ListContainer适配器
public class ListContainerProvider extends BaseItemProvider {
List<TestClass> list;
AbilitySlice as;
public ListContainerProvider(List<TestClass> list, AbilitySlice as) {
this.list = list;
this.as = as;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
if (list != null && i >= 0 && i < list.size()){
return list.get(i);
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
Text TITLE, AUTHOR, DATE;
DirectionalLayout dl;
if (component != null){
dl = (DirectionalLayout) component;
}else {
dl = (DirectionalLayout) LayoutScatter.getInstance(as).parse(ResourceTable.Layout_item_layout, null, false);
}
TestClass testClass = list.get(i);
TITLE = (Text) dl.findComponentById(ResourceTable.Id_text_title);
AUTHOR = (Text) dl.findComponentById(ResourceTable.Id_text_author);
DATE = (Text) dl.findComponentById(ResourceTable.Id_text_date);
TITLE.setText(testClass.getTitle());
AUTHOR.setText(testClass.getAuthor());
DATE.setText(testClass.getDate());
return dl;
}
}
3.MainAbilitySlice
public class MainAbilitySlice extends AbilitySlice {
private ListContainer LC_MAIN;
private List<TestClass> data_list = new ArrayList<>();
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
getData();
LC_MAIN = (ListContainer) findComponentById(ResourceTable.Id_lc_main);
LC_MAIN.setItemProvider(new ListContainerProvider(data_list, this));
LC_MAIN.setItemClickedListener(new ListContainer.ItemClickedListener() {
@Override
public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
new ToastDialog(getContext())
.setContentText("点击了" + String.valueOf(i + 1))
.show();
}
});
}
private void getData() {
for (int i = 1; i <= 100; i++){
data_list.add(new TestClass("新闻标题" + i, "作者" + i, "日期" + i));
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
最终效果图
小伙伴们觉得有收获记得点个赞哦!