# MultiType **Repository Path**: HarmonyOS-tpc/MultiType ## Basic Information - **Project Name**: MultiType - **Description**: Easier and more flexible to create multiple types for ListContainer. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 1 - **Created**: 2021-04-15 - **Last Updated**: 2025-02-06 ## Categories & Tags **Categories**: harmonyos-listview **Tags**: None ## README # MultiType MultiType : Easier and more flexible to create multiple types for ListContainer.Previously, when we need to develop a complex ListContainer, it is difficult and troublesome work. Once we need to add a new item type, we have to go to the original adapter file and modify some old codes carefully, and these adapter classes will get more complicated. MultiType provides convenient way to easily develop intricate ListContainers. With this library, we could insert a different kind of elements without changing any old itemprovider codes and make them more readable. # MultiType includes : * Normal Feature * Communicate with binder feature * Test Payload Feature * More Api's Playground Feature # Usage Instructions ================== The following core classes are the essential interface to MultiType: Normal Feature Normal feature demonstrates that with use of multitype adapter, user can set the customized directional layout backgound as listcotainer item. Gif, text and image can be support is available in this type. Create a class implement the provider, for example: `ListContainer listContainer = (ListContainer) rootContainer.findComponentById(ResourceTable.Id_list_main); listContainer.setItemProvider(new MultiTypeAdapter( itemsList) { @Override public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) { Component result = convertView; if (result == null) { result = LayoutScatter.getInstance(NormalSlice.this) .parse(ResourceTable.Layout_item_image, parent, false); } ViewCache viewCache = (ViewCache) result.getTag(); if (viewCache == null) { viewCache = new ViewCache(result); result.setTag(viewCache); } viewCache.getDirectionalLayout(ResourceTable.Id_directional_layout_normal); viewCache.getDirectionalLayout(ResourceTable.Id_directional_layout_image_outer); viewCache.getNormalTextView().setText("小艾大人赛高"); viewCache.getTextView().setText("Hello World"); viewCache.getImageView().setPixelMap(ResourceTable.Media_avatar); viewCache.getImageOuterNormalView().setPixelMap(ResourceTable.Media_icon); return result; } ------ Communicate with binder feature Communicate with binder feature demonstrates the simple use of listcontainer with text item and background as directionalLayout. listContainer.setItemProvider(new MultiTypeAdapter( itemsList) { @Override public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) { Component result = convertView; if (result == null) { result = LayoutScatter.getInstance(SimpleSlice.this) .parse(ResourceTable.Layout_communicate_with_binder_item, parent, false); } ViewCache viewCache = (ViewCache) result.getTag(); if (viewCache == null) { viewCache = new ViewCache(result); result.setTag(viewCache); } viewCache.getTextView().setText(itemsList.get(position)); viewCache.getTextView().setClickedListener(listener -> ViewUtil.showTips(getContext(), "Item " + position)); return result; } }); ------ Test Payload Feature Test Payload feature provides the elements with multiple text components including system current time. listContainer.setItemProvider(new MultiTypeAdapter( itemsList) { @Override public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) { Component result = convertView; if (result == null) { result = LayoutScatter.getInstance(TestPayloadSlice.this) .parse(ResourceTable.Layout_item_test_payload, parent, false); } ViewCache viewCache = (ViewCache) result.getTag(); if (viewCache == null) { viewCache = new ViewCache(result); result.setTag(viewCache); } viewCache.getLastTextView().setText("currentTimeMillis: " + System.currentTimeMillis()); viewCache.getDirectionalLayoutLine(); return result; } }); ------ More Api's Playground Feature More api's playground feature : In this feature listcontainer elements can be dynamically add, remove and clear from UI. listContainer.setItemProvider(new MultiTypeAdapter( itemsList) { @Override public Component getMyView(int position, Component convertView, ComponentContainer parent, String data) { Component result = convertView; if (result == null) { result = LayoutScatter.getInstance(MoreApisPlaygoundSlice.this) .parse(ResourceTable.Layout_communicate_with_binder_item, parent, false); } ViewCache viewCache = (ViewCache) result.getTag(); if (viewCache == null) { viewCache = new ViewCache(result); result.setTag(viewCache); } viewCache.getTextView().setText(itemsList.get(position)); return result; } }); ------ MultiTypeAdapter: It is a main class. Here BaseItemProvider is implementated which creates the each row in the list container. User can pass the custom layout from the class where adapter is implemented. getComponent method returns the component for each item in the listcontainer. public Component getComponent(int position, Component convertView, ComponentContainer parent) Please refer the sample app which is an app for showing different types of listcontainers. Multiple types of listcontainers can be implemented using the single adapter. That's all, you're good to go! # Installation Instructions 1.For using MultiType module in sample app, Add the dependencies in entry/build.gradle as below : dependencies { implementation project(path: ':library') } 2.Using the library.har, make sure to add library.har file in the entry/libs folder. Modify the dependencies in the entry/build.gradle file. dependencies { implementation fileTree(dir: 'libs', include: [' *.jar', ' *.har']) } 3. For using MultiType from a remote repository in separate application, add the below dependencies : Modify entry build.gradle as below : ``` dependencies { implementation 'io.openharmony.tpc.thirdlib:MultiType:1.0.2' } ``` # License Copyright 2017 drakeet. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.