1# Container Component Development<a name="EN-US_TOPIC_0000001052810390"></a> 2 3Container components are capable of containing UI components and inherit from **UIViewGroup**. Components that are commonly used and need to contain child components are placed in the container class inheritance structure. For example, you need to call the **Add** function to add information such as time statistics and icons to **UIAnalogClock**. 4 5**Figure 1** Structure of common container components<a name="fig1931013319293"></a> 6![](figures/structure-of-common-container-components.png "structure-of-common-container-components") 7 8The **RootView**, **UIAbstractScroll**, and **UIPicker** components inherit from **UIViewGroup**, and the **UIList**, **UIScrollView**, and **UISwipeView** components inherit from **UIAbstractScroll**. 9 10## UIViewGroup<a name="section145471898812"></a> 11 12## When to Use<a name="section0916112362216"></a> 13 14**UIViewGroup** is a base class for container components. For example, you can call the functions in this class to add, remove, and insert container components. Also, you can call the **Add** function to add child components for a container component. You need to set the position information for child components in a common container component. The position information is the coordinates relative to those of their parent component. The following figure shows the tree structure of components. 15 16**Figure 2** Component tree structure<a name="fig279970183012"></a> 17![](figures/component-tree-structure.png "component-tree-structure") 18 19As shown in the figure, the container component **ViewGroup1** and the component **View1** are added to **RootView**, the component **View2** and the container component **ViewGroup2** are added to **ViewGroup1**, and then the component **View3** \(as a sibling of **View1**\) is also added to **ViewGroup1**. 20 21- Rendering: During rendering of a container component, you need to call the **OnDraw** function on all its child components to update them. 22- Coordinates: As the position information of child components is the coordinates relative to those of their parent components, the system calculates and displays the absolute coordinates of child components during rendering. 23- Tree structure traversing: The **UIViewGroup** class provides the functions below to traverse, search for, and manage the component tree. 24 25## Available APIs<a name="section12641756192212"></a> 26 27**Table 1** Available functions in ViewGroup 28 29<a name="table15959122392211"></a> 30<table><thead align="left"><tr id="row14957132342220"><th class="cellrowborder" valign="top" width="64.73%" id="mcps1.2.3.1.1"><p id="p3957182302217"><a name="p3957182302217"></a><a name="p3957182302217"></a>Function</p> 31</th> 32<th class="cellrowborder" valign="top" width="35.27%" id="mcps1.2.3.1.2"><p id="p1595742312219"><a name="p1595742312219"></a><a name="p1595742312219"></a>Description</p> 33</th> 34</tr> 35</thead> 36<tbody><tr id="row14957123162218"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p89572023102216"><a name="p89572023102216"></a><a name="p89572023102216"></a>virtual void Add(UIView* view)</p> 37</td> 38<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p1495720236223"><a name="p1495720236223"></a><a name="p1495720236223"></a>Adds a child component.</p> 39</td> 40</tr> 41<tr id="row895892310224"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p16958192315227"><a name="p16958192315227"></a><a name="p16958192315227"></a>virtual void Insert(UIView* prevView, UIView* insertView)</p> 42</td> 43<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p1958223152210"><a name="p1958223152210"></a><a name="p1958223152210"></a>Inserts a child component.</p> 44</td> 45</tr> 46<tr id="row395862315229"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p16958142314223"><a name="p16958142314223"></a><a name="p16958142314223"></a>virtual void Remove(UIView* view)</p> 47</td> 48<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p2095862319224"><a name="p2095862319224"></a><a name="p2095862319224"></a>Removes a child component.</p> 49</td> 50</tr> 51<tr id="row195832352212"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p3958152362219"><a name="p3958152362219"></a><a name="p3958152362219"></a>virtual void RemoveAll()</p> 52</td> 53<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p10958132318225"><a name="p10958132318225"></a><a name="p10958132318225"></a>Removes all child components.</p> 54</td> 55</tr> 56<tr id="row895818232223"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p9958202322214"><a name="p9958202322214"></a><a name="p9958202322214"></a>virtual void GetTargetView(const Point& point, UIView** last)</p> 57</td> 58<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p79584234227"><a name="p79584234227"></a><a name="p79584234227"></a>Obtains the target view.</p> 59</td> 60</tr> 61<tr id="row795917235227"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p5958162362213"><a name="p5958162362213"></a><a name="p5958162362213"></a>virtual void MoveChildByOffset(int16_t x, int16_t y)</p> 62</td> 63<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p18958142315225"><a name="p18958142315225"></a><a name="p18958142315225"></a>Moves a child component by a specified offset.</p> 64</td> 65</tr> 66<tr id="row1395912314227"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p209591223172217"><a name="p209591223172217"></a><a name="p209591223172217"></a>UIView* GetChildrenHead() const</p> 67</td> 68<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p895942313224"><a name="p895942313224"></a><a name="p895942313224"></a>Obtains the first child view in a view group.</p> 69</td> 70</tr> 71<tr id="row7959192317226"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p89591423122213"><a name="p89591423122213"></a><a name="p89591423122213"></a>UIView* GetChildrenTail() const</p> 72</td> 73<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p17959132382217"><a name="p17959132382217"></a><a name="p17959132382217"></a>Obtains the last child view in a view group.</p> 74</td> 75</tr> 76<tr id="row39591423142214"><td class="cellrowborder" valign="top" width="64.73%" headers="mcps1.2.3.1.1 "><p id="p6959623182212"><a name="p6959623182212"></a><a name="p6959623182212"></a>virtual UIView* GetChildById(const char* id) const override</p> 77</td> 78<td class="cellrowborder" valign="top" width="35.27%" headers="mcps1.2.3.1.2 "><p id="p095932319222"><a name="p095932319222"></a><a name="p095932319222"></a>Obtains a child view based on its ID.</p> 79</td> 80</tr> 81</tbody> 82</table> 83 84## How to Develop<a name="section5412161692311"></a> 85 861. Create **ULLabelButton** instances and set their coordinates. 87 88 ``` 89 UILabelButton* btn1 = new UILabelButton(); 90 btn1->SetPosition(0, 0, 100, 50); 91 btn1->SetText("btn1"); 92 93 UILabelButton* btn2 = new UILabelButton(); 94 btn2->SetPosition(50, 50, 100, 50); 95 btn2->SetText("btn2"); 96 97 UILabelButton* btn3 = new UILabelButton(); 98 btn3->SetPosition(100, 100, 100, 50); 99 btn3->SetText("btn3"); 100 ``` 101 1022. Create a **UIViewGroup** instance and set its coordinates. 103 104 ``` 105 UIViewGroup* group = new UIViewGroup(); 106 group->SetPosition(0, 0, 300, 300); 107 ``` 108 1093. Add the **ULLabelButton** instances to **UIViewGroup**. 110 111 ``` 112 group->Add(btn1); 113 group->Add(btn2); 114 group->Add(btn3); 115 ``` 116 1174. The following figure shows the effect of adding view instances to a **ViewGroup**. 118 119 **Figure 3** Effect of adding view instances to a ViewGroup<a name="fig33041856184219"></a> 120 ![](figures/effect-of-adding-view-instances-to-a-viewgroup.png "effect-of-adding-view-instances-to-a-viewgroup") 121 122 123## UIScrollView<a name="section174961523161315"></a> 124 125## When to Use<a name="section8937101902413"></a> 126 127**UIScrollView** provides scrolling container components, which enable child components to scroll upwards, downwards, leftwards, and rightwards upon a touch event. This class also supports horizontal and vertical cursor display. 128 129## Available APIs<a name="section14789133142420"></a> 130 131**Table 2** Available functions in ScrollView 132 133<a name="table73572052411"></a> 134<table><thead align="left"><tr id="row432202014249"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p432142092415"><a name="p432142092415"></a><a name="p432142092415"></a>Function</p> 135</th> 136<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p4323208245"><a name="p4323208245"></a><a name="p4323208245"></a>Description</p> 137</th> 138</tr> 139</thead> 140<tbody><tr id="row13392032417"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p632182022417"><a name="p632182022417"></a><a name="p632182022417"></a>void ScrollBy(int16_t xDistance, int16_t yDistance)</p> 141</td> 142<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p13333208242"><a name="p13333208242"></a><a name="p13333208242"></a>Scrolls a view.</p> 143</td> 144</tr> 145<tr id="row1633132032415"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1533320172413"><a name="p1533320172413"></a><a name="p1533320172413"></a>void SetScrollbarWidth(uint8_t width)</p> 146</td> 147<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p173322015241"><a name="p173322015241"></a><a name="p173322015241"></a>Sets the scrollbar width.</p> 148</td> 149</tr> 150<tr id="row033152012416"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p173382042417"><a name="p173382042417"></a><a name="p173382042417"></a>void SetHorizontalScrollState(bool state)</p> 151</td> 152<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p103362042411"><a name="p103362042411"></a><a name="p103362042411"></a>Sets the horizontal scrolling state.</p> 153</td> 154</tr> 155<tr id="row03362011242"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p333152032413"><a name="p333152032413"></a><a name="p333152032413"></a>bool GetHorizontalScrollState() const</p> 156</td> 157<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p9331220192418"><a name="p9331220192418"></a><a name="p9331220192418"></a>Checks whether horizontal scrolling is allowed.</p> 158</td> 159</tr> 160<tr id="row1933520142413"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1833520192410"><a name="p1833520192410"></a><a name="p1833520192410"></a>void SetVerticalScrollState(bool state)</p> 161</td> 162<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p12331520132415"><a name="p12331520132415"></a><a name="p12331520132415"></a>Sets the vertical scrolling state.</p> 163</td> 164</tr> 165<tr id="row183318200243"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p63317207248"><a name="p63317207248"></a><a name="p63317207248"></a>bool GetVerticalScrollState() const</p> 166</td> 167<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1133020112417"><a name="p1133020112417"></a><a name="p1133020112417"></a>Checks whether vertical scrolling is allowed.</p> 168</td> 169</tr> 170<tr id="row43462072418"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p193313203245"><a name="p193313203245"></a><a name="p193313203245"></a>void SetXScrollBarVisible(bool state)</p> 171</td> 172<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1833102017242"><a name="p1833102017242"></a><a name="p1833102017242"></a>Sets whether the x-axis scrollbar is visible.</p> 173</td> 174</tr> 175<tr id="row1834720132417"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p8341620162410"><a name="p8341620162410"></a><a name="p8341620162410"></a>void SetYScrollBarVisible(bool state)</p> 176</td> 177<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p163462092412"><a name="p163462092412"></a><a name="p163462092412"></a>Sets whether the y-axis scrollbar is visible.</p> 178</td> 179</tr> 180<tr id="row13482082411"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p33415208244"><a name="p33415208244"></a><a name="p33415208244"></a>void RegisterScrollListener(OnScrollListener* scrollListener)</p> 181</td> 182<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p934112002417"><a name="p934112002417"></a><a name="p934112002417"></a>Registers the scrolling callback class.</p> 183</td> 184</tr> 185<tr id="row934192014242"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p18341020132410"><a name="p18341020132410"></a><a name="p18341020132410"></a>void RefreshScrollBar()</p> 186</td> 187<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p234520192417"><a name="p234520192417"></a><a name="p234520192417"></a>Refreshes the scrollbar.</p> 188</td> 189</tr> 190<tr id="row63422072418"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p8341620172411"><a name="p8341620172411"></a><a name="p8341620172411"></a>virtual void OnScrollStart() {}</p> 191</td> 192<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p193452052417"><a name="p193452052417"></a><a name="p193452052417"></a>Called when scrolling starts.</p> 193</td> 194</tr> 195<tr id="row93432017244"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p143416203242"><a name="p143416203242"></a><a name="p143416203242"></a>virtual void OnScrollEnd() {}</p> 196</td> 197<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p534152018243"><a name="p534152018243"></a><a name="p534152018243"></a>Called when scrolling ends.</p> 198</td> 199</tr> 200<tr id="row33532012247"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p1834172052419"><a name="p1834172052419"></a><a name="p1834172052419"></a>uint8_t GetScrollState() const</p> 201</td> 202<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1346205242"><a name="p1346205242"></a><a name="p1346205242"></a>Obtains the scrolling state.</p> 203</td> 204</tr> 205<tr id="row1535142022415"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p173532082413"><a name="p173532082413"></a><a name="p173532082413"></a>void SetScrollState(uint8_t state)</p> 206</td> 207<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1235102016242"><a name="p1235102016242"></a><a name="p1235102016242"></a>Sets the scrolling state.</p> 208</td> 209</tr> 210</tbody> 211</table> 212 213## How to Develop<a name="section1769754422417"></a> 214 215Add two buttons as child components and display horizontal and vertical cursors. 216 217``` 218scrollView* scroll = new UIScrollView(); 219scroll->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full); 220scroll->SetPosition(0,0, 200, 200); 221scroll->SetXScrollBarVisible(true); 222scroll->SetYScrollBarVisible(true); 223UILabelButton* button1 = new UILabelButton(); 224button1->SetText("button1"); 225button1->SetPosition(0, 0, 300, 300); 226UILabelButton* button2 = new UILabelButton(); 227button2->SetText("button2"); 228button2->SetPosition(0, 300, 300, 300); 229scroll->Add(button1); 230scroll->Add(button2); 231``` 232 233**Figure 4** Scrolling effect in both horizontal and vertical directions<a name="fig99017432114"></a> 234![](figures/scrolling-effect-in-both-horizontal-and-vertical-directions.gif "scrolling-effect-in-both-horizontal-and-vertical-directions") 235 236