目录:
UltimateProvider(适配器)简单快速的使用ListContainer而不需要写Provider。此框架是CHGAdapter的鸿蒙版本
UltimateProvider
UltimateProvider(Adapter)简单快速的使用ListContainer而不需要写Provider。此框架是CHGAdapter的鸿蒙版本
安卓(CHGAdapter)
Java:https://github.com/chenhaigang888/CHGAdapter_android
Kotlin:https://github.com/chenhaigang888/CHGAdapter_Kotlin
ios(CHGAdapter)
oc:https://github.com/chenhaigang888/CHGAdapter
swift:https://github.com/chenhaigang888/CHGAdapter_swift
HarmonyOS(UltimateProvider)
java:https://github.com/chenhaigang888/UltimateProvider
如有使用方面的问题或者交流请加QQ群:494648687
例
1.简单的显示(显示一种布局)
文件SongSlice.java
public class SongSlice extends AbilitySlice { private ListContainer listContainer; @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_song); listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer); listContainer.setItemProvider(new UltimateProvider(getSongs(),getContext())); } List getSongs(){ List list = new ArrayList(); for (int i=0; i<100; i++) { list.add(new SongModel("歌曲名称:"+i,"歌手:"+i)); } return list; } }
文件SongModel.java模型
public class SongModel implements Model { private String name; private String singer; @Override public int getResources(int position) { return ResourceTable.Layout_song_item; } @Override public Class getHolderClass(int position) { return SongViewHolder.class; } }
文件SongViewHolder.java模型
public class SongViewHolder extends ViewHolder<SongModel> { private Image icon; private Text songName; private Text songer; public SongViewHolder(EventTransmissionListener eventTransmissionListener, Component component, UltimateProvider provider) { super(eventTransmissionListener, component,provider); icon = (Image) findComponentById(ResourceTable.Id_icon); songName = (Text) findComponentById(ResourceTable.Id_songName); songer = (Text) findComponentById(ResourceTable.Id_songer); } @Override public void onDataBound() { songName.setText(getModel().getName()); songer.setText(getModel().getSinger()); } }
效果1:
2.简单的显示(显示多种布局)
RecommendSlice.java
public class RecommendSlice extends AbilitySlice { private ListContainer listContainer; @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_recommend); listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer); listContainer.setItemProvider(new UltimateProvider<Model>(getData(),getContext())); } List getData(){ List list = new ArrayList(); for (int i=0; i<100; i++) { if (i %2 == 0) { list.add(new SongModel("歌曲名称:"+i,"歌手名称:"+i)); } else { list.add(new AlbumModel("专辑名称:"+i,"歌手")); } } return list; } }
文件AlbumModel.java模型
public class AlbumModel implements Model { private String name; private String songer; @Override public int getResources(int position) { return ResourceTable.Layout_album_item; } @Override public Class getHolderClass(int position) { return AlbumViewHolder.class; } }
文件AlbumViewHolder.java
public class AlbumViewHolder extends ViewHolder<AlbumModel> { private Text name; private Text songer; public AlbumViewHolder(EventTransmissionListener eventTransmissionListener, Component component, UltimateProvider provider) { super(eventTransmissionListener, component,provider); name = (Text) findComponentById(ResourceTable.Id_name); songer = (Text) findComponentById(ResourceTable.Id_songer); } @Override public void onDataBound() { name.setText(getModel().getName()); songer.setText(getModel().getSonger()); } }
效果2:
3.嵌套ListContainer
NestedListAbility.java
public class NestedListSlice extends AbilitySlice { private ListContainer listContainer; @Override protected void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_nested_list); listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer); listContainer.setItemProvider(new UltimateProvider<Model>(getData(), getContext())); } List getData() { List list = new ArrayList(); for (int i=0; i<100; i++) { if (i%5 == 0) { list.add(new SongModel("歌曲名称:"+i,"歌手:"+i)); } else if (i%5 == 1) { list.add(new AlbumModel("推荐专辑:"+i,"歌手:"+i)); } else if (i%5 == 2) { list.add(creageMusicData(i,i%2)); } else { list.add(creageHybridData()); } } return list; } /*创建推荐内容*/ MusicModel creageMusicData(int position,int type){ List list = new ArrayList(); String title = type == 0 ? "推荐歌曲":"推荐专辑"; for (int i=0; i< 100; i++) { if (type == 0) { list.add(new NestedSongModel("推荐歌曲:"+i,"歌手:"+i)); } else if(type == 1){ list.add(new NestedAlbumModel("推荐专辑:"+i,"歌手:"+i)); } } return new MusicModel(title + position,list); } /*创建推荐内容*/ MusicModel creageHybridData(){ List list = new ArrayList(); for (int i=0; i< 100; i++) { if (i %2 == 0) { list.add(new NestedSongModel("推荐歌曲:"+i,"歌手:"+i)); } else { list.add(new NestedAlbumModel("推荐专辑:"+i,"歌手:"+i)); } } return new MusicModel("推荐的歌曲+专辑",list); } }
文件MusicModel.java模型
public class MusicModel implements Model { private String title; private List data; @Override public int getResources(int position) { return ResourceTable.Layout_music_item; } @Override public Class getHolderClass(int position) { return MusicViewHolder.class; } }
文件MusicViewHolder.java
public class MusicViewHolder extends ViewHolder<MusicModel> { private Text title; private ListContainer listContainer; private UltimateProvider ultimateProvider; public MusicViewHolder(EventTransmissionListener eventTransmissionListener, Component component,UltimateProvider provider) { super(eventTransmissionListener, component,provider); title = (Text) findComponentById(ResourceTable.Id_title); listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer); ultimateProvider = new UltimateProvider(null,getContext()); } @Override public void onDataBound() { title.setText(getModel().getTitle()); ultimateProvider.setModels(getModel().getData()); listContainer.setItemProvider(ultimateProvider); } }
效果3:
作者: mb5fed9a0375f80
想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com/