preface

This paper mainly introduces the use of ListContainer control in hongmeng application development, which involves the following knowledge points of Hongmeng SDK.

  • ListContainer
  • TaskDispatcher
  • EventHandler

Let’s cut to the chase.

ListContainer

ListContainer is equivalent to The Android Listview and will be one of the most commonly used controls in our daily development. Why is it the same as Listview? Not only is the effect basically the same, but the Api design and code implementation are basically the same

ListItemProvider.java

public class ListItemProvider extends RecycleItemProvider { private List<String> data; private Context context; private LayoutScatter scatter; public ListItemProvider(Context context, List<String> data) { this.context = context; this.data = data; this.scatter = LayoutScatter.getInstance(context); } @Override public int getCount() { return data ! = null ? data.size() : 0; } @Override public long getItemId(int i) { return i; } @Override public Component getComponent(int i, Component component, ComponentContainer componentContainer) { System.out.println("ListItemProvider-getComponent-" + i); ViewHolder viewHolder = null; if (component == null) { component = scatter.parse(ResourceTable.Layout_list_item, null, false); viewHolder = new ViewHolder((ComponentContainer) component); component.setTag(viewHolder); } else { viewHolder = (ViewHolder) component.getTag(); } viewHolder.left.setText(data.get(i)); viewHolder.right.setText(data.get(i)); return component; } static class ViewHolder { Text left; Text right; public ViewHolder(ComponentContainer container) { left = (Text) container.findComponentById(ResourceTable.Id_left); right = (Text) container.findComponentById(ResourceTable.Id_right); }}}Copy the code

Does the implementation of this class look familiar? This is the Adapter of Listview. This is not the same as the official demo, but it should be understood by android students in a minute. ListContainer basically copies the control design of Listview, including the Listview template reuse feature.

list_item.xml

<? The XML version = "1.0" encoding = "utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:orientation="horizontal"> <DependentLayout ohos:width="0" ohos:height="match_parent" ohos:weight="1" ohos:background_element="#ff0000" > <Text ohos:id="$+id:left" ohos:width="match_content" ohos:height="match_content" ohos:text_size="16vp" ohos:text_color="#ffffff" ohos:center_in_parent="true" /> </DependentLayout> <DependentLayout ohos:width="0" ohos:height="match_parent" ohos:weight="1" ohos:background_element="#0000ff" > <Text ohos:id="$+id:right" ohos:width="match_content" ohos:height="match_content" ohos:text_size="26vp" ohos:text_color="#ffffff" ohos:center_in_parent="true" /> </DependentLayout> </DirectionalLayout>Copy the code

This is the layout file for Item, not expanded.

list.xml

<? The XML version = "1.0" encoding = "utf-8"? > <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:orientation="vertical"> <ListContainer ohos:id="$+id:list" ohos:width="match_parent" ohos:height="match_parent" /> </DirectionalLayout>Copy the code

This is the main layout file, which is simply the ListContainer declaration.

MainAbilitySlice.java

public class MainAbilitySlice extends AbilitySlice { private ListContainer listContainer; private ListItemProvider itemProvider; private List<String> data = new ArrayList<>(); Private EventHandler Handler = new EventHandler(eventrunner.create (true)); Private EventHandler handler2 = new EventHandler(eventrunner.current ()) {@override protected void processEvent(InnerEvent event) { super.processEvent(event); System.out.println("2-" + Thread.currentThread().getName()); if (event.eventId == 0) { itemProvider.notifyDataChanged(); }}}; @Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_list); ListContainer = (listContainer) findComponentById(resourcetable.id_list); //1. ItemProvider = new ListItemProvider(this, data); / / 3. Will the Provider and ListContainer binding ListContainer setItemProvider (itemProvider); / / 4 set of item. Click the monitor listContainer setItemClickedListener (new listContainer. ItemClickedListener () {@ Override public void onItemClicked(ListContainer listContainer, Component component, int i, long l) { } }); // Simulate real time to get data, Handler. postTask(new Runnable() {@override public void run() {Override public void run() { System.out.println("1-" + Thread.currentThread().getName()); List<String> temp = new ArrayList<>(); for (int i = 0; i < 30; i++) { temp.add("" + (i + 1)); } data.addAll(temp); itemProvider.notifyDataChanged(); }}, 3000); / / way 2: using EventHandler and TaskDispatcher cooperate createParallelTaskDispatcher (" 123 ", TaskPriority.DEFAULT).delayDispatch(new Runnable() { @Override public void run() { System.out.println("1-" + Thread.currentThread().getName()); List<String> temp = new ArrayList<>(); for (int i = 0; i < 30; i++) { temp.add("" + (i + 1)); } data.addAll(temp); handler2.sendEvent(0); }}, 3000); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); }}Copy the code

MainAbilitySlice is equivalent to a Fragment, and the final implementation is here. The important steps have been commented out and are basically the Listview usage flow. Now that simple ListContainer is complete, let’s take a look at EventHandler and TaskDispatcher.

EventHandler

EventHandler is an Android handler, but it is not the same as EventHandler. It is not the same as EventHandler. Detailed introduction next time write a special introduction.

TaskDispatcher

What does TaskDispatcher look like in Android? It doesn’t really have a sibling, it’s more like an alternative to AsyncTask on Android, but the design and usage are very different. TaskDispatcher is a TaskDispatcher that is the basic interface to Ability to distribute tasks, hiding the implementation details of the thread in which the task resides. The following are several implementations of TaskDispatcher.

  • GlobalTaskDispatcher: Global concurrent task dispatcher obtained by the Ability implementation getGlobalTaskDispatcher(). This works when there is no connection between tasks. An application has only one GlobalTaskDispatcher, which is destroyed at the end of the application.

  • ParallelTaskDispatcher: concurrent tasks dispenser, by the Ability to perform createParallelTaskDispatcher () to create and return. Unlike GlobalTaskDispatcher, ParallelTaskDispatcher is not globally unique and can create more than one. The developer needs to hold the object reference when creating or destroying the Dispatcher.

  • Dispenser SerialTaskDispatcher: serial task, by the Ability to perform createSerialTaskDispatcher () to create and return. All the tasks distributed by the distributor are executed sequentially, but the threads that execute them are not fixed. If parallel tasks are to be performed, ParallelTaskDispatcher or GlobalTaskDispatcher should be used instead of creating multiple SerialTaskDispatchers. If there are no dependencies between tasks, use GlobalTaskDispatcher. Its creation and destruction are managed by the developer, who needs to hold the object reference for the duration of its use.

  • SpecTaskDispatcher: Proprietary task dispatcher, bound to a task dispatcher on a proprietary thread. The existing proprietary thread is the main thread. Both UITaskDispatcher and MainTaskDispatcher belong to SpecTaskDispatcher. UITaskDispatcher is recommended.

UITaskDispatcher: A proprietary task dispatcher bound to the main application thread, created and returned by the getUITaskDispatcher() execution of Ability. All tasks distributed by the distributor are executed sequentially on the main thread, which is destroyed at the end of the application.

The above words are official documents, just want to tell you these knowledge points. It is a task distribution with a thread pool attached to it to help application developers easily complete asynchronous tasks.

conclusion

Support Hongmeng, support Hongmeng ecology. This is the real domestic goods, want to make friends, we make a friend, study progress together, write bad, please forgive me.