1e41f4b71Sopenharmony_ci# Grid 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Grid** component consists of cells formed by rows and columns. You can specify the cells where items are located to form various layouts. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Child Components 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciOnly the [GridItem](ts-container-griditem.md) child component is allowed, with support for [if/else](../../../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../../../quick-start/arkts-rendering-control-foreach.md), [LazyForEach](../../../quick-start/arkts-rendering-control-lazyforeach.md), and [Repeat](../../../quick-start/arkts-new-rendering-control-repeat.md) rendering control. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci> **NOTE** 15e41f4b71Sopenharmony_ci> 16e41f4b71Sopenharmony_ci> Below are the rules for calculating the indexes of the child components of **Grid**: 17e41f4b71Sopenharmony_ci> 18e41f4b71Sopenharmony_ci> The index increases in ascending order of child components. 19e41f4b71Sopenharmony_ci> 20e41f4b71Sopenharmony_ci> In the **if/else** statement, only the child components in the branch where the condition is met participate in the index calculation. 21e41f4b71Sopenharmony_ci> 22e41f4b71Sopenharmony_ci> In the **ForEach**, **LazyForEach**, or **Repeat** statement, the indexes of all expanded child nodes are calculated. 23e41f4b71Sopenharmony_ci> 24e41f4b71Sopenharmony_ci> After changes occur in [if/else](../../../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../../../quick-start/arkts-rendering-control-foreach.md), [LazyForEach](../../../quick-start/arkts-rendering-control-lazyforeach.md), or [Repeat](../../../quick-start/arkts-new-rendering-control-repeat.md), the indexes of the child nodes are updated. 25e41f4b71Sopenharmony_ci> 26e41f4b71Sopenharmony_ci> The child component that has the **visibility** attribute set to **Hidden** or **None** is included in the index calculation. 27e41f4b71Sopenharmony_ci> 28e41f4b71Sopenharmony_ci> The child component that has the **visibility** attribute set to **None** is not displayed, but still takes up the corresponding cell. 29e41f4b71Sopenharmony_ci> 30e41f4b71Sopenharmony_ci> The child component that has the **position** attribute set is displayed in the corresponding cell, offset by the distance specified by **position** relative to the upper left corner of the grid. This child component does not scroll with the corresponding cell and is not displayed after the corresponding cell extends beyond the display range of the grid. 31e41f4b71Sopenharmony_ci> 32e41f4b71Sopenharmony_ci> When there is a gap between child components, it is filled as much as possible based on the current display area. Therefore, the relative position of grid items may change as the grid scrolls. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci## APIs 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ciGrid(scroller?: Scroller, layoutOptions?: GridLayoutOptions) 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**Parameters** 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 45e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 46e41f4b71Sopenharmony_ci| scroller | [Scroller](ts-container-scroll.md#scroller) | No | Controller, which can be bound to scrollable components.<br>**NOTE**<br>The scroller cannot be bound to other scrollable components, such as [List](ts-container-list.md), [Grid](ts-container-grid.md), or [Scroll](ts-container-scroll.md).| 47e41f4b71Sopenharmony_ci| layoutOptions<sup>10+</sup> | [GridLayoutOptions](#gridlayoutoptions10) | No| Grid layout options.| 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci## GridLayoutOptions<sup>10+</sup> 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciDefines the layout options. In this API, **irregularIndexes** and **onGetIrregularSizeByIndex** can be used for grids where either **rowsTemplate** or **columnsTemplate** is set. You can specify an index array and set the number of rows and columns to be occupied by a grid item with the specified index. For details, see Example 3. **onGetRectByIndex** can be used for grids where both **rowsTemplate** and **columnsTemplate** are set. It specifies the position and size for the grid item with the specified index. For details, see Example 1. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**Parameters** 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 60e41f4b71Sopenharmony_ci| ----- | ------- | ---- | --------------------- | 61e41f4b71Sopenharmony_ci| regularSize | [number, number] | Yes | Number of rows and columns occupied by a grid item with regular size. The only supported value is **[1, 1]**, meaning that the grid item occupies one row and one column.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 62e41f4b71Sopenharmony_ci| irregularIndexes | number[] | No | Indexes of the grid item with an irregular size in the grid. When **onGetIrregularSizeByIndex** is not set, the grid item specified in this parameter occupies an entire row of the grid that scrolls vertically or an entire column of the grid that scrolls horizontally.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 63e41f4b71Sopenharmony_ci| onGetIrregularSizeByIndex | (index: number) => [number, number] | No | Number of rows and columns occupied by the grid item with an irregular size. This parameter is used together with **irregularIndexes**. In versions earlier than API version 12, the vertical scrolling grid does not support grid items spanning multiple rows, and the horizontal scrolling grid does not support grid items spanning multiple columns.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 64e41f4b71Sopenharmony_ci| onGetRectByIndex<sup>11+</sup> | (index: number) => [number, number,number,number] | No | Position and size of the grid item with the specified index, in the format of [rowStart,columnStart,rowSpan,columnSpan],<br>where **rowStart** indicates the row start position, **columnStart** indicates the column start position,<br>**rowSpan** indicates the number of rows occupied by the grid item, and **columnSpan** indicates the number of columns occupied by the grid item. Their values are unitless.<br>The values of **rowStart** and **columnStart** are natural numbers greater than or equal to 0. If a negative value is set, the default value **0** is used.<br>The values of **rowSpan** and **columnSpan** are natural numbers greater than or equal to 1. If a decimal is set, it is rounded down. If the decimal set is less than 1, the value **1** is used.<br>**NOTE**<br>Case 1: If a grid item finds that the start position specified for it is already occupied, it searches for an available start position from left to right and from top to bottom, starting from position [0,0].<br>Case 2: If any space other than the start position specified for a grid item is occupied, the grid item is displayed within the available space left.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci## Attributes 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciIn addition to [universal attributes](ts-universal-attributes-size.md) and [scrollable component common attributes](ts-container-scrollable-common.md#attributes), the following attributes are also supported. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci### columnsTemplate 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_cicolumnsTemplate(value: string) 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciSets the number of columns, fixed column width, or minimum column width of the grid. If this attribute is not set, one column will be used. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciFor example, **'1fr 1fr 2fr'** indicates three columns, with the first column taking up 1/4 of the parent component's full width, the second column 1/4, and the third column 2/4. 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**columnsTemplate('repeat(auto-fit, track-size)')**: The layout automatically calculates the number of columns and the actual column width, while adhering to the minimum column width specified with **track-size**. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**columnsTemplate('repeat(auto-fill, track-size)')**: The layout automatically calculates the number of columns based on the fixed column width specified with **track-size**. 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**columnsTemplate('repeat(auto-stretch, track-size)')**: The layout uses **columnsGap** to define the minimum gap between columns and automatically calculates the number of columns and the actual gap size based on the fixed column width specified with **track-size**. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**repeat**, **auto-fit**, **auto-fill**, and **auto-stretch** are keywords. **track-size** indicates the column width, in the unit of px, vp (default), %, or any valid digit. The value must be greater than or equal to one valid column width.<br> 85e41f4b71Sopenharmony_ciIn **auto-stretch** mode, **track-size** must be a valid column width value, in the unit of px, vp, or any valid digit; percentage values (%) are not supported. 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ciFor visual reference, see [Example 8](#example-8). 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ciIf this attribute is set to **'0fr'**, the column width is 0, and grid item in the column is not displayed. If this attribute is set to any other invalid value, the grid item is displayed as one column. 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci**Parameters** 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 98e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------------------- | 99e41f4b71Sopenharmony_ci| value | string | Yes | Number of columns or minimum column width of the grid.| 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci### rowsTemplate 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_cirowsTemplate(value: string) 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ciSets the number of rows, fixed row height, or minimum row height of the grid. If this attribute is not set, one row will be used. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ciFor example, **'1fr 1fr 2fr'** indicates three rows, with the first row taking up 1/4 of the parent component's full height, the second row 1/4, and the third row 2/4. 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**rowsTemplate('repeat(auto-fit, track-size)')**: The layout automatically calculates the number of rows and the actual row height, while adhering to the minimum row height specified with **track-size**. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci**rowsTemplate('repeat(auto-fill, track-size)')**: The layout automatically calculates the number of rows based on the fixed row height specified with **track-size**. 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**rowsTemplate('repeat(auto-stretch, track-size)')**: The layout uses **rowsGap** to define the minimum gap between rows and automatically calculates the number of rows and the actual gap size based on the fixed row height specified with **track-size**. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**repeat**, **auto-fit**, **auto-fill**, and **auto-stretch** are keywords. **track-size** indicates the row height, in the unit of px, vp (default), %, or any valid digit. The value must be greater than or equal to one valid row height.<br> 116e41f4b71Sopenharmony_ciIn **auto-stretch** mode, **track-size** must be a valid row height value, in the unit of px, vp, or any valid digit; percentage values (%) are not supported. 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ciIf this attribute is set to **'0fr'**, the row width is 0, and grid item in the row is not displayed. If this attribute is set to any other invalid value, the grid item is displayed as one row. 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**Parameters** 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 127e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------------------- | 128e41f4b71Sopenharmony_ci| value | string | Yes | Number of rows or minimum row height of the grid.| 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci> **NOTE** 131e41f4b71Sopenharmony_ci> 132e41f4b71Sopenharmony_ci> Depending on the settings of the **rowsTemplate** and **columnsTemplate** attributes, the **Grid** component supports the following layout modes: 133e41f4b71Sopenharmony_ci> 134e41f4b71Sopenharmony_ci> 1. **rowsTemplate** and **columnsTemplate** are both set 135e41f4b71Sopenharmony_ci> 136e41f4b71Sopenharmony_ci> - The **Grid** component displays only elements in a fixed number of rows and columns and cannot be scrolled. 137e41f4b71Sopenharmony_ci> - In this mode, the following attributes do not take effect: **layoutDirection**, **maxCount**, **minCount**, and **cellLength**. 138e41f4b71Sopenharmony_ci> - If the width and height of a grid are not set, the grid adapts to the size of its parent component by default. 139e41f4b71Sopenharmony_ci> - The size of the grid rows and columns is the size of the grid content area minus the gap between rows and columns. It is allocated based on the proportion of each row and column. 140e41f4b71Sopenharmony_ci> - By default, the grid items fill the entire grid. 141e41f4b71Sopenharmony_ci> 142e41f4b71Sopenharmony_ci> 2. Either **rowsTemplate** or **columnsTemplate** is set 143e41f4b71Sopenharmony_ci> 144e41f4b71Sopenharmony_ci> - The **Grid** component arranges elements in the specified direction and allows for scrolling to display excess elements. 145e41f4b71Sopenharmony_ci> - If **columnsTemplate** is set, the component scrolls vertically, the main axis runs vertically, and the cross axis runs horizontally. 146e41f4b71Sopenharmony_ci> - If **rowsTemplate** is set, the component scrolls horizontally, the main axis runs horizontally, and the cross axis runs vertically. 147e41f4b71Sopenharmony_ci> - In this mode, the following attributes do not take effect: **layoutDirection**, **maxCount**, **minCount**, and **cellLength**. 148e41f4b71Sopenharmony_ci> - The cross axis size of the grid is the cross axis size of the grid content area minus the gaps along the cross axis. It is allocated based on the proportion of each row and column. 149e41f4b71Sopenharmony_ci> - The main axis size of the grid is the maximum height of all grid items in the cross axis direction of the current grid. 150e41f4b71Sopenharmony_ci> 151e41f4b71Sopenharmony_ci> 3. Neither **rowsTemplate** nor **columnsTemplate** is set 152e41f4b71Sopenharmony_ci> 153e41f4b71Sopenharmony_ci> - The **Grid** component arranges elements in the direction specified by **layoutDirection**. The number of columns is jointly determined by the grid width, width of the first element, **minCount**, **maxCount**, and **columnsGap**. 154e41f4b71Sopenharmony_ci> - The number of rows is jointly determined by the grid height, height of the first element, **cellLength**, and **rowsGap**. Elements outside the determined range of rows and columns are not displayed and cannot be viewed through scrolling. 155e41f4b71Sopenharmony_ci> - In this mode, only the following attributes take effect: **layoutDirection**, **maxCount**, **minCount**, **cellLength**, **editMode**, **columnsGap**, and **rowsGap**. 156e41f4b71Sopenharmony_ci> - When **layoutDirection** is set to **Row**, child components are arranged from left to right. When a row is full, a new row will be added. If the remaining height is insufficient, no more elements will be laid out, and the whole content is centered at the top. 157e41f4b71Sopenharmony_ci> - When **layoutDirection** is set to **Column**, elements are arranged column by column from top to bottom. If the remaining height is insufficient, no more elements will be laid out, and the whole content is centered at the top. 158e41f4b71Sopenharmony_ci> - If there are no grid items in the grid, the width and height of the grid are set to 0. 159e41f4b71Sopenharmony_ci> 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci### columnsGap 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_cicolumnsGap(value: Length) 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ciSets the gap between columns. A value less than 0 evaluates to the default value. 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**Parameters** 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 174e41f4b71Sopenharmony_ci| ------ | ---------------------------- | ---- | ---------------------------- | 175e41f4b71Sopenharmony_ci| value | [Length](ts-types.md#length) | Yes | Gap between columns.<br>Default value: **0**| 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci### rowsGap 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_cirowsGap(value: Length) 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ciSets the gap between rows. A value less than 0 evaluates to the default value. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**Parameters** 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 190e41f4b71Sopenharmony_ci| ------ | ---------------------------- | ---- | ---------------------------- | 191e41f4b71Sopenharmony_ci| value | [Length](ts-types.md#length) | Yes | Gap between rows.<br>Default value: **0**| 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci### scrollBar 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ciscrollBar(value: BarState) 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ciSets the scrollbar state. 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**Parameters** 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 206e41f4b71Sopenharmony_ci| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ | 207e41f4b71Sopenharmony_ci| value | [BarState](ts-appendix-enums.md#barstate) | Yes | Scrollbar state.<br>Default value: **BarState.Auto**<br>**NOTE**<br>In API version 9 and earlier versions, the default value is **BarState.Off**. Since API version 10, the default value is **BarState.Auto**.| 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci### scrollBarColor 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ciscrollBarColor(value: Color | number | string) 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ciSets the scrollbar color. 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci**Parameters** 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 222e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | -------------- | 223e41f4b71Sopenharmony_ci| value | [Color](ts-appendix-enums.md#color) \| number \| string | Yes | Scrollbar color.<br>Default value: **'\#182431'** (40% opacity)| 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci### scrollBarWidth 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ciscrollBarWidth(value: number | string) 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ciSets the scrollbar width. This attribute cannot be set in percentage. After the width is set, the scrollbar is displayed with the set width in normal state and pressed state. If the set width exceeds the height of the **Grid** component on the main axis, the scrollbar reverts to the default width. 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci**Parameters** 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 238e41f4b71Sopenharmony_ci| ------ | -------------------------- | ---- | ----------------------------------------- | 239e41f4b71Sopenharmony_ci| value | number \| string | Yes | Scrollbar width.<br>Default value: **4**<br>Unit: vp| 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci### cachedCount 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_cicachedCount(value: number) 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ciSets the number of grid items to be preloaded (cached). It works only in [LazyForEach](../../../quick-start/arkts-rendering-control-lazyforeach.md) and [Repeat](../../../quick-start/arkts-new-rendering-control-repeat.md) with the **virtualScroll** option enabled. A value less than 0 evaluates to the default value. <!--Del-->For details, see [Minimizing White Blocks During Swiping](../../../performance/arkts-performance-improvement-recommendation.md#minimizing-white-blocks-during-swiping).<!--DelEnd--> 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ciThe number of the grid items to be cached before and after the currently displayed one equals the value of **cachedCount** multiplied by the number of columns. 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ciIn [LazyForEach](../../../quick-start/arkts-rendering-control-lazyforeach.md) and [Repeat](../../../quick-start/arkts-new-rendering-control-repeat.md) with the **virtualScroll** option enabled, grid items that are outside the display and cache range will be released. 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**Parameters** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 258e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- | 259e41f4b71Sopenharmony_ci| value | number | Yes | Number of grid items to be preloaded (cached).<br>Default value: **1**| 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci### editMode<sup>8+</sup> 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_cieditMode(value: boolean) 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ciSets whether to enable edit mode. In edit mode, the user can drag the [grid items](ts-container-griditem.md) in the **Grid** component. 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci**Parameters** 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 274e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------------------------------- | 275e41f4b71Sopenharmony_ci| value | boolean | Yes | Whether to enable edit mode.<br>Default value: **false**| 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci### layoutDirection<sup>8+</sup> 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_cilayoutDirection(value: GridDirection) 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ciSets the main axis direction of the grid. 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci**Parameters** 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 290e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | ---- | ---------------------------------------------- | 291e41f4b71Sopenharmony_ci| value | [GridDirection](#griddirection8) | Yes | Main axis direction of the grid.<br>Default value: **GridDirection.Row**| 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci### maxCount<sup>8+</sup> 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_cimaxCount(value: number) 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ciSets the maximum number of rows or columns that can be displayed. A value less than 1 evaluates to the default value. 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciWhen **layoutDirection** is **Row** or **RowReverse**, the value indicates the maximum number of columns that can be displayed. 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ciWhen **layoutDirection** is **Column** or **ColumnReverse**, the value indicates the maximum number of rows that can be displayed. 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ciIf the value of **maxCount** is smaller than that of **minCount**, the default values of **maxCount** and **minCount** are used. 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci**Parameters** 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 312e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------------------------- | 313e41f4b71Sopenharmony_ci| value | number | Yes | Maximum number of rows or columns that can be displayed.<br>Default value: **Infinity**| 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci### minCount<sup>8+</sup> 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ciminCount(value: number) 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ciSets the minimum number of rows or columns that can be displayed. A value less than 1 evaluates to the default value. 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ciWhen **layoutDirection** is **Row** or **RowReverse**, the value indicates the minimum number of columns that can be displayed. 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ciWhen **layoutDirection** is **Column** or **ColumnReverse**, the value indicates the minimum number of rows that can be displayed. 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci**Parameters** 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 332e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------------------- | 333e41f4b71Sopenharmony_ci| value | number | Yes | Minimum number of rows or columns that can be displayed.<br>Default value: **1**| 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci### cellLength<sup>8+</sup> 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_cicellLength(value: number) 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ciSets the height per row or width per column. 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ciWhen **layoutDirection** is **Row** or **RowReverse**, the value indicates the height per row. 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ciWhen **layoutDirection** is **Column** or **ColumnReverse**, the value indicates the width per column. 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci**Parameters** 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 352e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------- | 353e41f4b71Sopenharmony_ci| value | number | Yes | Height per row or width per column.<br>Default value: size of the first element<br>Unit: vp| 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci### multiSelectable<sup>8+</sup> 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_cimultiSelectable(value: boolean) 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ciSets whether to enable multiselect. When multiselect is enabled, you can use the **selected** attribute and **onSelect** event to obtain the selected status of grid items; you can also set the [style](./ts-universal-attributes-polymorphic-style.md) for the selected state (by default, no style is set for the selected state). 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**Parameters** 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 368e41f4b71Sopenharmony_ci| ------ | ------- | ---- | ------------------------------------------------------------ | 369e41f4b71Sopenharmony_ci| value | boolean | Yes | Whether to enable multiselect.<br>Default value: **false**<br>**false**: Multiselect is disabled. **true**: Multiselect is disabled.| 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci### supportAnimation<sup>8+</sup> 372e41f4b71Sopenharmony_ci 373e41f4b71Sopenharmony_cisupportAnimation(value: boolean) 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ciSets whether to enable animation. Currently, the grid item drag animation is supported. Animation is supported only in scrolling mode (only **rowsTemplate** or **columnsTemplate** is set).<br>Drag animations are only supported in grids with fixed size rules; scenarios involving spanning across rows or columns are not supported. 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 378e41f4b71Sopenharmony_ci 379e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 380e41f4b71Sopenharmony_ci 381e41f4b71Sopenharmony_ci**Parameters** 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 384e41f4b71Sopenharmony_ci| ------ | ------- | ---- | -------------------------------- | 385e41f4b71Sopenharmony_ci| value | boolean | Yes | Whether to enable animation.<br>Default value: **false**| 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ci### edgeEffect<sup>10+</sup> 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ciedgeEffect(value: EdgeEffect, options?: EdgeEffectOptions) 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ciSets the effect used when the scroll boundary is reached. 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ci**Parameters** 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 400e41f4b71Sopenharmony_ci| --------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 401e41f4b71Sopenharmony_ci| value | [EdgeEffect](ts-appendix-enums.md#edgeeffect) | Yes | Effect used when the scroll boundary is reached. The spring and shadow effects are supported.<br>Default value: **EdgeEffect.None**| 402e41f4b71Sopenharmony_ci| options<sup>11+</sup> | [EdgeEffectOptions](ts-container-scrollable-common.md#edgeeffectoptions11) | No | Whether to enable the scroll effect when the component content is smaller than the component itself. The value **{ alwaysEnabled: true }** means to enable the scroll effect, and **{ alwaysEnabled: false }** means the opposite.<br>Default value: **{ alwaysEnabled: false }**| 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci### enableScrollInteraction<sup>10+</sup> 405e41f4b71Sopenharmony_ci 406e41f4b71Sopenharmony_cienableScrollInteraction(value: boolean) 407e41f4b71Sopenharmony_ci 408e41f4b71Sopenharmony_ciSets whether to support scroll gestures. When this attribute is set to **false**, scrolling by finger or mouse is not supported, but the scrolling controller API is not affected. 409e41f4b71Sopenharmony_ci 410e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 411e41f4b71Sopenharmony_ci 412e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci**Parameters** 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 417e41f4b71Sopenharmony_ci| ------ | ------- | ---- | ----------------------------------- | 418e41f4b71Sopenharmony_ci| value | boolean | Yes | Whether to support scroll gestures.<br>Default value: **true**| 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci### nestedScroll<sup>10+</sup> 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_cinestedScroll(value: NestedScrollOptions) 423e41f4b71Sopenharmony_ci 424e41f4b71Sopenharmony_ciSets the nested scrolling options. You can set the nested scrolling mode in the forward and backward directions to implement scrolling linkage with the parent component. 425e41f4b71Sopenharmony_ci 426e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci**Parameters** 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 433e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | -------------- | 434e41f4b71Sopenharmony_ci| value | [NestedScrollOptions](ts-container-scrollable-common.md#nestedscrolloptions10) | Yes | Nested scrolling options.| 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci### friction<sup>10+</sup> 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_cifriction(value: number | Resource) 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ciSets the friction coefficient. It applies only to gestures in the scrolling area, and it affects only indirectly the scroll chaining during the inertial scrolling process. A value less than or equal to 0 evaluates to the default value. 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci**Parameters** 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 449e41f4b71Sopenharmony_ci| ------ | ---------------------------------------------------- | ---- | ----------------------------------------------------------- | 450e41f4b71Sopenharmony_ci| value | number \| [Resource](ts-types.md#resource) | Yes | Friction coefficient.<br>Default value: **0.9** for wearable devices and **0.6** for non-wearable devices<br>Since API version 11, the default value for non-wearable devices is **0.7**.<br>Since API version 12, the default value for non-wearable devices is **0.75**.| 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci### alignItems<sup>12+</sup> 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_cialignItems(alignment: Optional\<GridItemAlignment\>) 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ciSets the alignment mode of grid items in the grid. For details, see [Example 9](#example-9). 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 461e41f4b71Sopenharmony_ci 462e41f4b71Sopenharmony_ci**Parameters** 463e41f4b71Sopenharmony_ci 464e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 465e41f4b71Sopenharmony_ci| ---------- | ------ | ---- | ------------------------------- | 466e41f4b71Sopenharmony_ci| alignment | Optional\<[GridItemAlignment](#griditemalignment12)\> | Yes | Alignment mode of grid items in the grid.<br>Default value: **GridItemAlignment.DEFAULT**| 467e41f4b71Sopenharmony_ci 468e41f4b71Sopenharmony_ci## GridItemAlignment<sup>12+</sup> 469e41f4b71Sopenharmony_ci 470e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 471e41f4b71Sopenharmony_ci 472e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci| Name | Value| Description | 475e41f4b71Sopenharmony_ci| ------ |------| -------------------------------------- | 476e41f4b71Sopenharmony_ci| DEFAULT | 0 | Use the default alignment mode of the grid.| 477e41f4b71Sopenharmony_ci| STRETCH | 1 | Use the height of the tallest grid item in a row as the height for all other grid items in that row.| 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci 480e41f4b71Sopenharmony_ci> **NOTE** 481e41f4b71Sopenharmony_ci> 482e41f4b71Sopenharmony_ci> 1. The **STRETCH** option only takes effect in scrollable grids.<br> 483e41f4b71Sopenharmony_ci> 2. The **STRETCH** option takes effect only if each grid item in a row is of a regular size (occupying only one row and one column). It is not effective in scenarios where there are grid items spanning across rows or columns.<br> 484e41f4b71Sopenharmony_ci> 3. When **STRETCH** is used, only grid items without a set height will adopt the height of the tallest grid item in the current row; the height of grid items with a set height will remain unchanged.<br> 485e41f4b71Sopenharmony_ci> 4. When **STRETCH** is used, the grid undergoes an additional layout process, which may incur additional performance overhead. 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci## GridDirection<sup>8+</sup> 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci| Name |Value| Description | 494e41f4b71Sopenharmony_ci| ------ |------| -------------------------------------- | 495e41f4b71Sopenharmony_ci| Row | 0 | Horizontal layout, where the child components are arranged from left to right as the main axis runs along the rows.| 496e41f4b71Sopenharmony_ci| Column | 1 | Vertical layout, where the child components are arranged from top to bottom as the main axis runs down the columns.| 497e41f4b71Sopenharmony_ci| RowReverse | 2 | Reverse horizontal layout, where the child components are arranged from right to left as the main axis runs along the rows.| 498e41f4b71Sopenharmony_ci| ColumnReverse | 3 | Reverse vertical layout, where the child components are arranged from bottom up as the main axis runs down the columns.| 499e41f4b71Sopenharmony_ci 500e41f4b71Sopenharmony_ci> **NOTE** 501e41f4b71Sopenharmony_ci> 502e41f4b71Sopenharmony_ci> The default value of the universal attribute [clip](ts-universal-attributes-sharp-clipping.md) is **true** for the **Grid** component. 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci## Events 505e41f4b71Sopenharmony_ci 506e41f4b71Sopenharmony_ciIn addition to [universal events](ts-universal-events-click.md) and [scrollable component common events](ts-container-scrollable-common.md#events), the following events are also supported. 507e41f4b71Sopenharmony_ci 508e41f4b71Sopenharmony_ci### onScrollIndex 509e41f4b71Sopenharmony_ci 510e41f4b71Sopenharmony_cionScrollIndex(event: (first: number, last: number) => void) 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ciTriggered when the first or last item displayed in the grid changes, that is, when the index of either the first or last item changes. It is triggered once when the grid is initialized. 513e41f4b71Sopenharmony_ci 514e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 515e41f4b71Sopenharmony_ci 516e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 517e41f4b71Sopenharmony_ci 518e41f4b71Sopenharmony_ci**Parameters** 519e41f4b71Sopenharmony_ci 520e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 521e41f4b71Sopenharmony_ci| ------------------ | ------ | ---- | -------------------------------- | 522e41f4b71Sopenharmony_ci| first | number | Yes | Index of the first item of the grid.| 523e41f4b71Sopenharmony_ci| last<sup>10+</sup> | number | Yes | Index of the last item of the grid.| 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci### onItemDragStart<sup>8+</sup> 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_cionItemDragStart(event: (event: ItemDragInfo, itemIndex: number) => (() => any) \| void) 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ciTriggered when a grid item starts to be dragged. If **void** is returned, the drag operation cannot be performed. 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_ciThis event is triggered when the user long presses a grid item. 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ciDrag gesture recognition is also initiated by a long press, and the event processing mechanism prioritizes child component events. Therefore, when the grid item is bound to the long press gesture, it cannot be dragged. In light of this, if both long press and drag operations are required on the grid item, you can use the universal drag event. 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci**Parameters** 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 542e41f4b71Sopenharmony_ci| --------- | ------------------------------------- | ---- | ---------------------- | 543e41f4b71Sopenharmony_ci| event | [ItemDragInfo](ts-container-scrollable-common.md#itemdraginfo) | Yes | Information about the drag point. | 544e41f4b71Sopenharmony_ci| itemIndex | number | Yes | Index of the dragged item.| 545e41f4b71Sopenharmony_ci 546e41f4b71Sopenharmony_ci### onItemDragEnter<sup>8+</sup> 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_cionItemDragEnter(event: (event: ItemDragInfo) => void) 549e41f4b71Sopenharmony_ci 550e41f4b71Sopenharmony_ciTriggered when the dragged item enters the drop target of the grid. 551e41f4b71Sopenharmony_ci 552e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci**Parameters** 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 559e41f4b71Sopenharmony_ci| ------ | ------------------------------------- | ---- | -------------- | 560e41f4b71Sopenharmony_ci| event | [ItemDragInfo](ts-container-scrollable-common.md#itemdraginfo) | Yes | Information about the drag point.| 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci### onItemDragMove<sup>8+</sup> 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_cionItemDragMove(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number) => void) 565e41f4b71Sopenharmony_ci 566e41f4b71Sopenharmony_ciTriggered when the dragged item moves over the drop target of the grid. 567e41f4b71Sopenharmony_ci 568e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci**Parameters** 573e41f4b71Sopenharmony_ci 574e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 575e41f4b71Sopenharmony_ci| ----------- | ------------------------------------- | ---- | -------------- | 576e41f4b71Sopenharmony_ci| event | [ItemDragInfo](ts-container-scrollable-common.md#itemdraginfo) | Yes | Information about the drag point.| 577e41f4b71Sopenharmony_ci| itemIndex | number | Yes | Initial position of the dragged item.| 578e41f4b71Sopenharmony_ci| insertIndex | number | Yes | Index of the position to which the dragged item is dropped.| 579e41f4b71Sopenharmony_ci 580e41f4b71Sopenharmony_ci### onItemDragLeave<sup>8+</sup> 581e41f4b71Sopenharmony_ci 582e41f4b71Sopenharmony_cionItemDragLeave(event: (event: ItemDragInfo, itemIndex: number) => void) 583e41f4b71Sopenharmony_ci 584e41f4b71Sopenharmony_ciTriggered when the dragged item leaves the drop target of the grid. 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 589e41f4b71Sopenharmony_ci 590e41f4b71Sopenharmony_ci**Parameters** 591e41f4b71Sopenharmony_ci 592e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 593e41f4b71Sopenharmony_ci| --------- | ------------------------------------- | ---- | -------------------------- | 594e41f4b71Sopenharmony_ci| event | [ItemDragInfo](ts-container-scrollable-common.md#itemdraginfo) | Yes | Information about the drag point. | 595e41f4b71Sopenharmony_ci| itemIndex | number | Yes | Index of the dragged item.| 596e41f4b71Sopenharmony_ci 597e41f4b71Sopenharmony_ci### onItemDrop<sup>8+</sup> 598e41f4b71Sopenharmony_ci 599e41f4b71Sopenharmony_cionItemDrop(event: (event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => void) 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ciTriggered when the dragged item is dropped on the drop target of the grid. 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ci**Parameters** 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 610e41f4b71Sopenharmony_ci| ----------- | ------------------------------------- | ---- | -------------- | 611e41f4b71Sopenharmony_ci| event | [ItemDragInfo](ts-container-scrollable-common.md#itemdraginfo) | Yes | Information about the drag point.| 612e41f4b71Sopenharmony_ci| itemIndex | number | Yes | Initial position of the dragged item.| 613e41f4b71Sopenharmony_ci| insertIndex | number | Yes | Index of the position to which the dragged item is dropped.| 614e41f4b71Sopenharmony_ci| isSuccess | boolean | Yes | Whether the dragged item is successfully dropped. | 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci### onScrollBarUpdate<sup>10+</sup> 617e41f4b71Sopenharmony_ci 618e41f4b71Sopenharmony_cionScrollBarUpdate(event: (index: number, offset: number) => ComputedBarAttribute) 619e41f4b71Sopenharmony_ci 620e41f4b71Sopenharmony_ciTriggered when the first item displayed in the grid changes. You can use the callback to set the position and length of the scrollbar. 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ciThis API is intended solely for setting the scroll position of the grid. Avoid implementing service logic within this API. 623e41f4b71Sopenharmony_ci 624e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 625e41f4b71Sopenharmony_ci 626e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ci**Parameters** 629e41f4b71Sopenharmony_ci 630e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 631e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 632e41f4b71Sopenharmony_ci| index | number | Yes | Index of the first item of the grid. | 633e41f4b71Sopenharmony_ci| offset | number | Yes | Offset of the displayed first item relative to the start position of the grid, in vp.| 634e41f4b71Sopenharmony_ci 635e41f4b71Sopenharmony_ci**Return value** 636e41f4b71Sopenharmony_ci 637e41f4b71Sopenharmony_ci| Type | Description | 638e41f4b71Sopenharmony_ci| ----------------------------------------------------- | -------------------- | 639e41f4b71Sopenharmony_ci| [ComputedBarAttribute](#computedbarattribute10) | Position and length of the scrollbar.| 640e41f4b71Sopenharmony_ci 641e41f4b71Sopenharmony_ci### onReachStart<sup>10+</sup> 642e41f4b71Sopenharmony_ci 643e41f4b71Sopenharmony_cionReachStart(event: () => void) 644e41f4b71Sopenharmony_ci 645e41f4b71Sopenharmony_ciTriggered when the grid reaches the start position. 646e41f4b71Sopenharmony_ci 647e41f4b71Sopenharmony_ciThis event is triggered once when the grid is initialized and once when the grid scrolls to the start position. If the edge effect is set to a spring effect, this event is triggered once when the swipe passes the start position, and triggered again when the swipe rebounds back to the start position. 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci### onReachEnd<sup>10+</sup> 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_cionReachEnd(event: () => void) 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ciTriggered when the grid reaches the end position. 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ciIf the edge effect is set to a spring effect, this event is triggered once when the swipe passes the end position, and triggered again when the swipe rebounds back to the end position. 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci### onScrollFrameBegin<sup>10+</sup> 666e41f4b71Sopenharmony_ci 667e41f4b71Sopenharmony_cionScrollFrameBegin(event: (offset: number, state: ScrollState) => { offsetRemain: number }) 668e41f4b71Sopenharmony_ci 669e41f4b71Sopenharmony_ciTriggered when the grid starts to scroll. After the amount by which the grid will scroll is passed in, the event handler works out the amount by which the grid needs to scroll based on the real-world situation and returns the result. 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 674e41f4b71Sopenharmony_ci 675e41f4b71Sopenharmony_ci**Parameters** 676e41f4b71Sopenharmony_ci 677e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 678e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------- | ---- | -------------------------- | 679e41f4b71Sopenharmony_ci| offset | number | Yes | Amount to scroll by, in vp.| 680e41f4b71Sopenharmony_ci| state | [ScrollState](ts-container-list.md#scrollstate) | Yes | Current scroll state. | 681e41f4b71Sopenharmony_ci 682e41f4b71Sopenharmony_ci**Return value** 683e41f4b71Sopenharmony_ci 684e41f4b71Sopenharmony_ci| Type | Description | 685e41f4b71Sopenharmony_ci| ------------------------ | -------------------- | 686e41f4b71Sopenharmony_ci| { offsetRemain: number } | Actual amount by which the grid scrolls, in vp.| 687e41f4b71Sopenharmony_ci 688e41f4b71Sopenharmony_ci### onScrollStart<sup>10+</sup> 689e41f4b71Sopenharmony_ci 690e41f4b71Sopenharmony_cionScrollStart(event: () => void) 691e41f4b71Sopenharmony_ci 692e41f4b71Sopenharmony_ciTriggered when the grid starts scrolling initiated by the user's finger dragging the grid or its scrollbar. This event is also triggered when the animation contained in the scrolling triggered by [Scroller](ts-container-scroll.md#scroller) starts. 693e41f4b71Sopenharmony_ci 694e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 695e41f4b71Sopenharmony_ci 696e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 697e41f4b71Sopenharmony_ci 698e41f4b71Sopenharmony_ci### onScrollStop<sup>10+</sup> 699e41f4b71Sopenharmony_ci 700e41f4b71Sopenharmony_cionScrollStop(event: () => void) 701e41f4b71Sopenharmony_ci 702e41f4b71Sopenharmony_ciTriggered when the grid stops scrolling after the user's finger leaves the screen. This event is also triggered when the animation contained in the scrolling triggered by [Scroller](ts-container-scroll.md#scroller) stops. 703e41f4b71Sopenharmony_ci 704e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 705e41f4b71Sopenharmony_ci 706e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 707e41f4b71Sopenharmony_ci 708e41f4b71Sopenharmony_ci### onScroll<sup>(deprecated)</sup> 709e41f4b71Sopenharmony_cionScroll(event: (scrollOffset: number, scrollState: [ScrollState](ts-container-list.md#scrollstate)) => void) 710e41f4b71Sopenharmony_ci 711e41f4b71Sopenharmony_ciTriggered when the grid scrolls. 712e41f4b71Sopenharmony_ci 713e41f4b71Sopenharmony_ciThis API is supported since API version 10 and deprecated since API version 12. You are advised to use [onDidScroll](ts-container-scrollable-common.md#ondidscroll12) instead. 714e41f4b71Sopenharmony_ci 715e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 716e41f4b71Sopenharmony_ci 717e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 718e41f4b71Sopenharmony_ci 719e41f4b71Sopenharmony_ci**Parameters** 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 722e41f4b71Sopenharmony_ci| ------ | ------ | ------ | ------| 723e41f4b71Sopenharmony_ci| scrollOffset | number | Yes| Scroll offset of each frame. The offset is positive when the grid is scrolled up and negative when the grid is scrolled down.<br>Unit: vp| 724e41f4b71Sopenharmony_ci| scrollState | [ScrollState](ts-container-list.md#scrollstate) | Yes| Current scroll state.| 725e41f4b71Sopenharmony_ci 726e41f4b71Sopenharmony_ci## ComputedBarAttribute<sup>10+</sup> 727e41f4b71Sopenharmony_ci 728e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 729e41f4b71Sopenharmony_ci 730e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 731e41f4b71Sopenharmony_ci 732e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 733e41f4b71Sopenharmony_ci| ----------- | ------------ | ---- | ---- | ---------- | 734e41f4b71Sopenharmony_ci| totalOffset | number | No| No| Total offset of the grid content relative to the display area, in px. | 735e41f4b71Sopenharmony_ci| totalLength | number | No| No| Total length of the grid content, in px. | 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ci## Example 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci### Example 1 740e41f4b71Sopenharmony_ci 741e41f4b71Sopenharmony_ciThis example shows a grid with fixed number of rows and columns, for which you can use **onGetRectByIndex** in **GridLayoutOptions** to specify the position and size of grid items. 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci```ts 744e41f4b71Sopenharmony_ci// xxx.ets 745e41f4b71Sopenharmony_ci@Entry 746e41f4b71Sopenharmony_ci@Component 747e41f4b71Sopenharmony_cistruct GridExample { 748e41f4b71Sopenharmony_ci @State numbers1: String[] = ['0', '1', '2', '3', '4'] 749e41f4b71Sopenharmony_ci @State numbers2: String[] = ['0', '1','2','3','4','5'] 750e41f4b71Sopenharmony_ci 751e41f4b71Sopenharmony_ci layoutOptions3: GridLayoutOptions = { 752e41f4b71Sopenharmony_ci regularSize: [1, 1], 753e41f4b71Sopenharmony_ci onGetRectByIndex: (index: number) => { 754e41f4b71Sopenharmony_ci if (index == 0) 755e41f4b71Sopenharmony_ci return [0, 0, 1, 1] 756e41f4b71Sopenharmony_ci else if(index==1) 757e41f4b71Sopenharmony_ci return [0, 1, 2, 2] 758e41f4b71Sopenharmony_ci else if(index==2) 759e41f4b71Sopenharmony_ci return [0 ,3 ,3 ,3] 760e41f4b71Sopenharmony_ci else if(index==3) 761e41f4b71Sopenharmony_ci return [3, 0, 3, 3] 762e41f4b71Sopenharmony_ci else if(index==4) 763e41f4b71Sopenharmony_ci return [4, 3, 2, 2] 764e41f4b71Sopenharmony_ci else 765e41f4b71Sopenharmony_ci return [5, 5, 1, 1] 766e41f4b71Sopenharmony_ci } 767e41f4b71Sopenharmony_ci } 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ci build() { 770e41f4b71Sopenharmony_ci Column({ space: 5 }) { 771e41f4b71Sopenharmony_ci Grid() { 772e41f4b71Sopenharmony_ci ForEach(this.numbers1, (day: string) => { 773e41f4b71Sopenharmony_ci ForEach(this.numbers1, (day: string) => { 774e41f4b71Sopenharmony_ci GridItem() { 775e41f4b71Sopenharmony_ci Text(day) 776e41f4b71Sopenharmony_ci .fontSize(16) 777e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 778e41f4b71Sopenharmony_ci .width('100%') 779e41f4b71Sopenharmony_ci .height('100%') 780e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 781e41f4b71Sopenharmony_ci } 782e41f4b71Sopenharmony_ci }, (day: string) => day) 783e41f4b71Sopenharmony_ci }, (day: string) => day) 784e41f4b71Sopenharmony_ci } 785e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr 1fr 1fr 1fr') 786e41f4b71Sopenharmony_ci .rowsTemplate('1fr 1fr 1fr 1fr 1fr') 787e41f4b71Sopenharmony_ci .columnsGap(10) 788e41f4b71Sopenharmony_ci .rowsGap(10) 789e41f4b71Sopenharmony_ci .width('90%') 790e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 791e41f4b71Sopenharmony_ci .height(300) 792e41f4b71Sopenharmony_ci 793e41f4b71Sopenharmony_ci Text('Use of GridLayoutOptions: onGetRectByIndex').fontColor(0xCCCCCC).fontSize(9).width('90%') 794e41f4b71Sopenharmony_ci 795e41f4b71Sopenharmony_ci Grid(undefined, this.layoutOptions3) { 796e41f4b71Sopenharmony_ci ForEach(this.numbers2, (day: string) => { 797e41f4b71Sopenharmony_ci GridItem() { 798e41f4b71Sopenharmony_ci Text(day) 799e41f4b71Sopenharmony_ci .fontSize(16) 800e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 801e41f4b71Sopenharmony_ci .width('100%') 802e41f4b71Sopenharmony_ci .height("100%") 803e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 804e41f4b71Sopenharmony_ci } 805e41f4b71Sopenharmony_ci .height("100%") 806e41f4b71Sopenharmony_ci .width('100%') 807e41f4b71Sopenharmony_ci }, (day: string) => day) 808e41f4b71Sopenharmony_ci } 809e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr 1fr 1fr 1fr 1fr') 810e41f4b71Sopenharmony_ci .rowsTemplate('1fr 1fr 1fr 1fr 1fr 1fr') 811e41f4b71Sopenharmony_ci .columnsGap(10) 812e41f4b71Sopenharmony_ci .rowsGap(10) 813e41f4b71Sopenharmony_ci .width('90%') 814e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 815e41f4b71Sopenharmony_ci .height(300) 816e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 817e41f4b71Sopenharmony_ci } 818e41f4b71Sopenharmony_ci} 819e41f4b71Sopenharmony_ci``` 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_ci 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci### Example 2 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ciThis example shows a scrollable grid with all its scrolling attributes and events specified. 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci```ts 828e41f4b71Sopenharmony_ci// xxx.ets 829e41f4b71Sopenharmony_ci@Entry 830e41f4b71Sopenharmony_ci@Component 831e41f4b71Sopenharmony_cistruct GridExample { 832e41f4b71Sopenharmony_ci @State numbers: String[] = ['0', '1', '2', '3', '4'] 833e41f4b71Sopenharmony_ci scroller: Scroller = new Scroller() 834e41f4b71Sopenharmony_ci @State gridPosition: number = 0 // 0 indicates scrolling to the top of the grid, 1 indicates scrolling to the center, and 2 indicates scrolling to the bottom. 835e41f4b71Sopenharmony_ci 836e41f4b71Sopenharmony_ci build() { 837e41f4b71Sopenharmony_ci Column({ space: 5 }) { 838e41f4b71Sopenharmony_ci Text('scroll').fontColor(0xCCCCCC).fontSize(9).width('90%') 839e41f4b71Sopenharmony_ci Grid(this.scroller) { 840e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 841e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 842e41f4b71Sopenharmony_ci GridItem() { 843e41f4b71Sopenharmony_ci Text(day) 844e41f4b71Sopenharmony_ci .fontSize(16) 845e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 846e41f4b71Sopenharmony_ci .width('100%') 847e41f4b71Sopenharmony_ci .height(80) 848e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 849e41f4b71Sopenharmony_ci } 850e41f4b71Sopenharmony_ci }, (day: string) => day) 851e41f4b71Sopenharmony_ci }, (day: string) => day) 852e41f4b71Sopenharmony_ci } 853e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr 1fr 1fr 1fr') 854e41f4b71Sopenharmony_ci .columnsGap(10) 855e41f4b71Sopenharmony_ci .rowsGap(10) 856e41f4b71Sopenharmony_ci .friction(0.6) 857e41f4b71Sopenharmony_ci .enableScrollInteraction(true) 858e41f4b71Sopenharmony_ci .supportAnimation(false) 859e41f4b71Sopenharmony_ci .multiSelectable(false) 860e41f4b71Sopenharmony_ci .edgeEffect(EdgeEffect.Spring) 861e41f4b71Sopenharmony_ci .scrollBar(BarState.On) 862e41f4b71Sopenharmony_ci .scrollBarColor(Color.Grey) 863e41f4b71Sopenharmony_ci .scrollBarWidth(4) 864e41f4b71Sopenharmony_ci .width('90%') 865e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 866e41f4b71Sopenharmony_ci .height(300) 867e41f4b71Sopenharmony_ci .onScrollIndex((first: number, last: number) => { 868e41f4b71Sopenharmony_ci console.info(first.toString()) 869e41f4b71Sopenharmony_ci console.info(last.toString()) 870e41f4b71Sopenharmony_ci }) 871e41f4b71Sopenharmony_ci .onScrollBarUpdate((index: number, offset: number) => { 872e41f4b71Sopenharmony_ci console.info("XXX" + 'Grid onScrollBarUpdate,index : ' + index.toString() + ",offset" + offset.toString()) 873e41f4b71Sopenharmony_ci return { totalOffset: (index / 5) * (80 + 10) - offset, totalLength: 80 * 5 + 10 * 4 } 874e41f4b71Sopenharmony_ci }) // The sample code applies only to the current data source. If the data source changes, modify the code or delete this attribute. 875e41f4b71Sopenharmony_ci .onDidScroll((scrollOffset: number, scrollState: ScrollState) => { 876e41f4b71Sopenharmony_ci console.info(scrollOffset.toString()) 877e41f4b71Sopenharmony_ci console.info(scrollState.toString()) 878e41f4b71Sopenharmony_ci }) 879e41f4b71Sopenharmony_ci .onScrollStart(() => { 880e41f4b71Sopenharmony_ci console.info("XXX" + "Grid onScrollStart") 881e41f4b71Sopenharmony_ci }) 882e41f4b71Sopenharmony_ci .onScrollStop(() => { 883e41f4b71Sopenharmony_ci console.info("XXX" + "Grid onScrollStop") 884e41f4b71Sopenharmony_ci }) 885e41f4b71Sopenharmony_ci .onReachStart(() => { 886e41f4b71Sopenharmony_ci this.gridPosition = 0 887e41f4b71Sopenharmony_ci console.info("XXX" + "Grid onReachStart") 888e41f4b71Sopenharmony_ci }) 889e41f4b71Sopenharmony_ci .onReachEnd(() => { 890e41f4b71Sopenharmony_ci this.gridPosition = 2 891e41f4b71Sopenharmony_ci console.info("XXX" + "Grid onReachEnd") 892e41f4b71Sopenharmony_ci }) 893e41f4b71Sopenharmony_ci 894e41f4b71Sopenharmony_ci Button('next page') 895e41f4b71Sopenharmony_ci .onClick(() => {// Click to go to the next page. 896e41f4b71Sopenharmony_ci this.scroller.scrollPage({ next: true }) 897e41f4b71Sopenharmony_ci }) 898e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 899e41f4b71Sopenharmony_ci } 900e41f4b71Sopenharmony_ci} 901e41f4b71Sopenharmony_ci``` 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci 904e41f4b71Sopenharmony_ci 905e41f4b71Sopenharmony_ci### Example 3 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_ciThis example uses **irregularIndexes** and **onGetIrregularSizeByIndex** in **GridLayoutOptions**. 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_ci```ts 910e41f4b71Sopenharmony_ci// xxx.ets 911e41f4b71Sopenharmony_ci@Entry 912e41f4b71Sopenharmony_ci@Component 913e41f4b71Sopenharmony_cistruct GridExample { 914e41f4b71Sopenharmony_ci @State numbers: String[] = ['0', '1', '2', '3', '4'] 915e41f4b71Sopenharmony_ci scroller: Scroller = new Scroller() 916e41f4b71Sopenharmony_ci layoutOptions1: GridLayoutOptions = { 917e41f4b71Sopenharmony_ci regularSize: [1, 1], // Only [1, 1] is supported. 918e41f4b71Sopenharmony_ci irregularIndexes: [0, 6], // The grid item whose indexes are 0 and 6 occupies one row. 919e41f4b71Sopenharmony_ci } 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci layoutOptions2: GridLayoutOptions = { 922e41f4b71Sopenharmony_ci regularSize: [1, 1], 923e41f4b71Sopenharmony_ci irregularIndexes: [0, 7], // The number of columns occupied by the grid item whose indexes are 0 and 7 is specified by onGetIrregularSizeByIndex. 924e41f4b71Sopenharmony_ci onGetIrregularSizeByIndex: (index: number) => { 925e41f4b71Sopenharmony_ci if (index === 0) { 926e41f4b71Sopenharmony_ci return [1, 5] 927e41f4b71Sopenharmony_ci } 928e41f4b71Sopenharmony_ci return [1, index % 6 + 1] 929e41f4b71Sopenharmony_ci } 930e41f4b71Sopenharmony_ci } 931e41f4b71Sopenharmony_ci 932e41f4b71Sopenharmony_ci build() { 933e41f4b71Sopenharmony_ci Column({ space: 5 }) { 934e41f4b71Sopenharmony_ci Grid(this.scroller, this.layoutOptions1) { 935e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 936e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 937e41f4b71Sopenharmony_ci GridItem() { 938e41f4b71Sopenharmony_ci Text(day) 939e41f4b71Sopenharmony_ci .fontSize(16) 940e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 941e41f4b71Sopenharmony_ci .width('100%') 942e41f4b71Sopenharmony_ci .height(80) 943e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 944e41f4b71Sopenharmony_ci }.selectable(false) 945e41f4b71Sopenharmony_ci }, (day: string) => day) 946e41f4b71Sopenharmony_ci }, (day: string) => day) 947e41f4b71Sopenharmony_ci } 948e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr 1fr 1fr 1fr') 949e41f4b71Sopenharmony_ci .columnsGap(10) 950e41f4b71Sopenharmony_ci .rowsGap(10) 951e41f4b71Sopenharmony_ci .multiSelectable(true) 952e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 953e41f4b71Sopenharmony_ci .width('90%') 954e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 955e41f4b71Sopenharmony_ci .height(300) 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci Text('scroll').fontColor(0xCCCCCC).fontSize(9).width('90%') 958e41f4b71Sopenharmony_ci // The grid does not scroll, and undefined is used to reserve space. 959e41f4b71Sopenharmony_ci Grid(undefined, this.layoutOptions2) { 960e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 961e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 962e41f4b71Sopenharmony_ci GridItem() { 963e41f4b71Sopenharmony_ci Text(day) 964e41f4b71Sopenharmony_ci .fontSize(16) 965e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 966e41f4b71Sopenharmony_ci .width('100%') 967e41f4b71Sopenharmony_ci .height(80) 968e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 969e41f4b71Sopenharmony_ci } 970e41f4b71Sopenharmony_ci }, (day: string) => day) 971e41f4b71Sopenharmony_ci }, (day: string) => day) 972e41f4b71Sopenharmony_ci } 973e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr 1fr 1fr 1fr') 974e41f4b71Sopenharmony_ci .columnsGap(10) 975e41f4b71Sopenharmony_ci .rowsGap(10) 976e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 977e41f4b71Sopenharmony_ci .width('90%') 978e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 979e41f4b71Sopenharmony_ci .height(300) 980e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 981e41f4b71Sopenharmony_ci } 982e41f4b71Sopenharmony_ci} 983e41f4b71Sopenharmony_ci``` 984e41f4b71Sopenharmony_ci 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci 987e41f4b71Sopenharmony_ci### Example 4 988e41f4b71Sopenharmony_ci 989e41f4b71Sopenharmony_ciExample of using **nestedScroll** and **onScrollFrameBegin**: 990e41f4b71Sopenharmony_ci 991e41f4b71Sopenharmony_ci```ts 992e41f4b71Sopenharmony_ci@Entry 993e41f4b71Sopenharmony_ci@Component 994e41f4b71Sopenharmony_cistruct GridExample { 995e41f4b71Sopenharmony_ci @State colors: number[] = [0xFFC0CB, 0xDA70D6, 0x6B8E23, 0x6A5ACD, 0x00FFFF, 0x00FF7F] 996e41f4b71Sopenharmony_ci @State numbers: number[] = [] 997e41f4b71Sopenharmony_ci @State translateY: number = 0 998e41f4b71Sopenharmony_ci private scroller: Scroller = new Scroller() 999e41f4b71Sopenharmony_ci private gridScroller: Scroller = new Scroller() 1000e41f4b71Sopenharmony_ci private touchDown: boolean = false 1001e41f4b71Sopenharmony_ci private listTouchDown: boolean = false 1002e41f4b71Sopenharmony_ci private scrolling: boolean = false 1003e41f4b71Sopenharmony_ci 1004e41f4b71Sopenharmony_ci aboutToAppear() { 1005e41f4b71Sopenharmony_ci for (let i = 0; i < 100; i++) { 1006e41f4b71Sopenharmony_ci this.numbers.push(i) 1007e41f4b71Sopenharmony_ci } 1008e41f4b71Sopenharmony_ci } 1009e41f4b71Sopenharmony_ci 1010e41f4b71Sopenharmony_ci build() { 1011e41f4b71Sopenharmony_ci Stack() { 1012e41f4b71Sopenharmony_ci Column() { 1013e41f4b71Sopenharmony_ci Row() { 1014e41f4b71Sopenharmony_ci Text('Head') 1015e41f4b71Sopenharmony_ci } 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci Column() { 1018e41f4b71Sopenharmony_ci List({ scroller: this.scroller }) { 1019e41f4b71Sopenharmony_ci ListItem() { 1020e41f4b71Sopenharmony_ci Grid() { 1021e41f4b71Sopenharmony_ci GridItem() { 1022e41f4b71Sopenharmony_ci Text('GoodsTypeList1') 1023e41f4b71Sopenharmony_ci } 1024e41f4b71Sopenharmony_ci .backgroundColor(this.colors[0]) 1025e41f4b71Sopenharmony_ci .columnStart(0) 1026e41f4b71Sopenharmony_ci .columnEnd(1) 1027e41f4b71Sopenharmony_ci 1028e41f4b71Sopenharmony_ci GridItem() { 1029e41f4b71Sopenharmony_ci Text('GoodsTypeList2') 1030e41f4b71Sopenharmony_ci } 1031e41f4b71Sopenharmony_ci .backgroundColor(this.colors[1]) 1032e41f4b71Sopenharmony_ci .columnStart(0) 1033e41f4b71Sopenharmony_ci .columnEnd(1) 1034e41f4b71Sopenharmony_ci 1035e41f4b71Sopenharmony_ci GridItem() { 1036e41f4b71Sopenharmony_ci Text('GoodsTypeList3') 1037e41f4b71Sopenharmony_ci } 1038e41f4b71Sopenharmony_ci .backgroundColor(this.colors[2]) 1039e41f4b71Sopenharmony_ci .columnStart(0) 1040e41f4b71Sopenharmony_ci .columnEnd(1) 1041e41f4b71Sopenharmony_ci 1042e41f4b71Sopenharmony_ci GridItem() { 1043e41f4b71Sopenharmony_ci Text('GoodsTypeList4') 1044e41f4b71Sopenharmony_ci } 1045e41f4b71Sopenharmony_ci .backgroundColor(this.colors[3]) 1046e41f4b71Sopenharmony_ci .columnStart(0) 1047e41f4b71Sopenharmony_ci .columnEnd(1) 1048e41f4b71Sopenharmony_ci 1049e41f4b71Sopenharmony_ci GridItem() { 1050e41f4b71Sopenharmony_ci Text('GoodsTypeList5') 1051e41f4b71Sopenharmony_ci } 1052e41f4b71Sopenharmony_ci .backgroundColor(this.colors[4]) 1053e41f4b71Sopenharmony_ci .columnStart(0) 1054e41f4b71Sopenharmony_ci .columnEnd(1) 1055e41f4b71Sopenharmony_ci } 1056e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 1057e41f4b71Sopenharmony_ci .columnsGap(15) 1058e41f4b71Sopenharmony_ci .rowsGap(10) 1059e41f4b71Sopenharmony_ci .rowsTemplate('1fr 1fr 1fr 1fr 1fr') 1060e41f4b71Sopenharmony_ci .columnsTemplate('1fr') 1061e41f4b71Sopenharmony_ci .width('100%') 1062e41f4b71Sopenharmony_ci .height(200) 1063e41f4b71Sopenharmony_ci } 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci ListItem() { 1066e41f4b71Sopenharmony_ci Grid(this.gridScroller) { 1067e41f4b71Sopenharmony_ci ForEach(this.numbers, (item: number) => { 1068e41f4b71Sopenharmony_ci GridItem() { 1069e41f4b71Sopenharmony_ci Text(item + '') 1070e41f4b71Sopenharmony_ci .fontSize(16) 1071e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 1072e41f4b71Sopenharmony_ci .width('100%') 1073e41f4b71Sopenharmony_ci .height('100%') 1074e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 1075e41f4b71Sopenharmony_ci } 1076e41f4b71Sopenharmony_ci .width('100%') 1077e41f4b71Sopenharmony_ci .height(40) 1078e41f4b71Sopenharmony_ci .shadow({ radius: 10, color: '#909399', offsetX: 1, offsetY: 1 }) 1079e41f4b71Sopenharmony_ci .borderRadius(10) 1080e41f4b71Sopenharmony_ci .translate({ x: 0, y: this.translateY }) 1081e41f4b71Sopenharmony_ci }, (item: string) => item) 1082e41f4b71Sopenharmony_ci } 1083e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr') 1084e41f4b71Sopenharmony_ci .friction(0.3) 1085e41f4b71Sopenharmony_ci .columnsGap(15) 1086e41f4b71Sopenharmony_ci .rowsGap(10) 1087e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 1088e41f4b71Sopenharmony_ci .width('100%') 1089e41f4b71Sopenharmony_ci .height('100%') 1090e41f4b71Sopenharmony_ci .layoutDirection(GridDirection.Column) 1091e41f4b71Sopenharmony_ci .nestedScroll({ 1092e41f4b71Sopenharmony_ci scrollForward: NestedScrollMode.PARENT_FIRST, 1093e41f4b71Sopenharmony_ci scrollBackward: NestedScrollMode.SELF_FIRST 1094e41f4b71Sopenharmony_ci }) 1095e41f4b71Sopenharmony_ci .onTouch((event: TouchEvent) => { 1096e41f4b71Sopenharmony_ci if (event.type == TouchType.Down) { 1097e41f4b71Sopenharmony_ci this.listTouchDown = true 1098e41f4b71Sopenharmony_ci } else if (event.type == TouchType.Up) { 1099e41f4b71Sopenharmony_ci this.listTouchDown = false 1100e41f4b71Sopenharmony_ci } 1101e41f4b71Sopenharmony_ci }) 1102e41f4b71Sopenharmony_ci } 1103e41f4b71Sopenharmony_ci } 1104e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 1105e41f4b71Sopenharmony_ci .edgeEffect(EdgeEffect.None) 1106e41f4b71Sopenharmony_ci .onTouch((event: TouchEvent) => { 1107e41f4b71Sopenharmony_ci if (event.type == TouchType.Down) { 1108e41f4b71Sopenharmony_ci this.touchDown = true 1109e41f4b71Sopenharmony_ci } else if (event.type == TouchType.Up) { 1110e41f4b71Sopenharmony_ci this.touchDown = false 1111e41f4b71Sopenharmony_ci } 1112e41f4b71Sopenharmony_ci }) 1113e41f4b71Sopenharmony_ci .onScrollFrameBegin((offset: number, state: ScrollState) => { 1114e41f4b71Sopenharmony_ci if (this.scrolling && offset > 0) { 1115e41f4b71Sopenharmony_ci let newOffset = this.scroller.currentOffset().yOffset 1116e41f4b71Sopenharmony_ci if (newOffset >= 590) { 1117e41f4b71Sopenharmony_ci this.gridScroller.scrollBy(0, offset) 1118e41f4b71Sopenharmony_ci return { offsetRemain: 0 } 1119e41f4b71Sopenharmony_ci } else if (newOffset + offset > 590) { 1120e41f4b71Sopenharmony_ci this.gridScroller.scrollBy(0, newOffset + offset - 590) 1121e41f4b71Sopenharmony_ci return { offsetRemain: 590 - newOffset } 1122e41f4b71Sopenharmony_ci } 1123e41f4b71Sopenharmony_ci } 1124e41f4b71Sopenharmony_ci return { offsetRemain: offset } 1125e41f4b71Sopenharmony_ci }) 1126e41f4b71Sopenharmony_ci .onScrollStart(() => { 1127e41f4b71Sopenharmony_ci if (this.touchDown && !this.listTouchDown) { 1128e41f4b71Sopenharmony_ci this.scrolling = true 1129e41f4b71Sopenharmony_ci } 1130e41f4b71Sopenharmony_ci }) 1131e41f4b71Sopenharmony_ci .onScrollStop(() => { 1132e41f4b71Sopenharmony_ci this.scrolling = false 1133e41f4b71Sopenharmony_ci }) 1134e41f4b71Sopenharmony_ci } 1135e41f4b71Sopenharmony_ci .width('100%') 1136e41f4b71Sopenharmony_ci .height('100%') 1137e41f4b71Sopenharmony_ci .padding({ left: 10, right: 10 }) 1138e41f4b71Sopenharmony_ci } 1139e41f4b71Sopenharmony_ci 1140e41f4b71Sopenharmony_ci Row() { 1141e41f4b71Sopenharmony_ci Text('Top') 1142e41f4b71Sopenharmony_ci .width(30) 1143e41f4b71Sopenharmony_ci .height(30) 1144e41f4b71Sopenharmony_ci .borderRadius(50) 1145e41f4b71Sopenharmony_ci } 1146e41f4b71Sopenharmony_ci .padding(5) 1147e41f4b71Sopenharmony_ci .borderRadius(50) 1148e41f4b71Sopenharmony_ci .backgroundColor('#ffffff') 1149e41f4b71Sopenharmony_ci .shadow({ radius: 10, color: '#909399', offsetX: 1, offsetY: 1 }) 1150e41f4b71Sopenharmony_ci .margin({ right: 22, bottom: 15 }) 1151e41f4b71Sopenharmony_ci .onClick(() => { 1152e41f4b71Sopenharmony_ci this.scroller.scrollTo({ xOffset: 0, yOffset: 0 }) 1153e41f4b71Sopenharmony_ci this.gridScroller.scrollTo({ xOffset: 0, yOffset: 0 }) 1154e41f4b71Sopenharmony_ci }) 1155e41f4b71Sopenharmony_ci } 1156e41f4b71Sopenharmony_ci .align(Alignment.BottomEnd) 1157e41f4b71Sopenharmony_ci } 1158e41f4b71Sopenharmony_ci} 1159e41f4b71Sopenharmony_ci``` 1160e41f4b71Sopenharmony_ci 1161e41f4b71Sopenharmony_ci 1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci### Example 5 1164e41f4b71Sopenharmony_ci 1165e41f4b71Sopenharmony_ci1. Set **editMode\(true\)** to enable edit mode, where the user can drag the grid items. 1166e41f4b71Sopenharmony_ci2. Through [onItemDragStart](#events), set the image to be displayed during dragging. 1167e41f4b71Sopenharmony_ci3. Through [onItemDrop](#events), obtain the initial position of the dragged item and the position to which the dragged item will be dropped. Through [onItemDrop](#events), complete the array position exchange logic. 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_ci> **NOTE** 1170e41f4b71Sopenharmony_ci> 1171e41f4b71Sopenharmony_ci> The drag and drop action is not displayed in the preview. 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ci```ts 1174e41f4b71Sopenharmony_ci@Entry 1175e41f4b71Sopenharmony_ci@Component 1176e41f4b71Sopenharmony_cistruct GridExample { 1177e41f4b71Sopenharmony_ci @State numbers: string[] = [] 1178e41f4b71Sopenharmony_ci scroller: Scroller = new Scroller() 1179e41f4b71Sopenharmony_ci @State text: string = 'drag' 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci @Builder pixelMapBuilder () { // Style for the drag event. 1182e41f4b71Sopenharmony_ci Column() { 1183e41f4b71Sopenharmony_ci Text(this.text) 1184e41f4b71Sopenharmony_ci .fontSize(16) 1185e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 1186e41f4b71Sopenharmony_ci .width(80) 1187e41f4b71Sopenharmony_ci .height(80) 1188e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 1189e41f4b71Sopenharmony_ci } 1190e41f4b71Sopenharmony_ci } 1191e41f4b71Sopenharmony_ci 1192e41f4b71Sopenharmony_ci aboutToAppear() { 1193e41f4b71Sopenharmony_ci for (let i = 1;i <= 15; i++) { 1194e41f4b71Sopenharmony_ci this.numbers.push(i + '') 1195e41f4b71Sopenharmony_ci } 1196e41f4b71Sopenharmony_ci } 1197e41f4b71Sopenharmony_ci 1198e41f4b71Sopenharmony_ci changeIndex(index1: number, index2: number) { // Exchange the array position. 1199e41f4b71Sopenharmony_ci let temp: string; 1200e41f4b71Sopenharmony_ci temp = this.numbers[index1]; 1201e41f4b71Sopenharmony_ci this.numbers[index1] = this.numbers[index2]; 1202e41f4b71Sopenharmony_ci this.numbers[index2] = temp; 1203e41f4b71Sopenharmony_ci } 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci build() { 1206e41f4b71Sopenharmony_ci Column({ space: 5 }) { 1207e41f4b71Sopenharmony_ci Grid(this.scroller) { 1208e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 1209e41f4b71Sopenharmony_ci GridItem() { 1210e41f4b71Sopenharmony_ci Text(day) 1211e41f4b71Sopenharmony_ci .fontSize(16) 1212e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 1213e41f4b71Sopenharmony_ci .width(80) 1214e41f4b71Sopenharmony_ci .height(80) 1215e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 1216e41f4b71Sopenharmony_ci } 1217e41f4b71Sopenharmony_ci }) 1218e41f4b71Sopenharmony_ci } 1219e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr 1fr') 1220e41f4b71Sopenharmony_ci .columnsGap(10) 1221e41f4b71Sopenharmony_ci .rowsGap(10) 1222e41f4b71Sopenharmony_ci .width('90%') 1223e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 1224e41f4b71Sopenharmony_ci .height(300) 1225e41f4b71Sopenharmony_ci .editMode(true) // Enable edit mode, where the user can drag the grid items. 1226e41f4b71Sopenharmony_ci .onItemDragStart((event: ItemDragInfo, itemIndex: number) => { // Triggered when a grid item starts to be dragged. 1227e41f4b71Sopenharmony_ci this.text = this.numbers[itemIndex] 1228e41f4b71Sopenharmony_ci return this.pixelMapBuilder() // Set the image to be displayed during dragging. 1229e41f4b71Sopenharmony_ci }) 1230e41f4b71Sopenharmony_ci .onItemDrop((event: ItemDragInfo, itemIndex: number, insertIndex: number, isSuccess: boolean) => { // Triggered when the dragged item is dropped on the drop target of the grid. 1231e41f4b71Sopenharmony_ci // If isSuccess is set to false, the item is dropped outside of the grid. If the value of insertIndex is greater than that of length, an item adding event occurs. 1232e41f4b71Sopenharmony_ci if (!isSuccess || insertIndex >= this.numbers.length) { 1233e41f4b71Sopenharmony_ci return 1234e41f4b71Sopenharmony_ci } 1235e41f4b71Sopenharmony_ci console.info('beixiang' + itemIndex + '', insertIndex + '') // itemIndex indicates the initial position of the dragged item. insertIndex indicates the position to which the dragged item will be dropped. 1236e41f4b71Sopenharmony_ci this.changeIndex(itemIndex, insertIndex) 1237e41f4b71Sopenharmony_ci }) 1238e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 1239e41f4b71Sopenharmony_ci } 1240e41f4b71Sopenharmony_ci} 1241e41f4b71Sopenharmony_ci``` 1242e41f4b71Sopenharmony_ci 1243e41f4b71Sopenharmony_ciBelow are some examples. 1244e41f4b71Sopenharmony_ci 1245e41f4b71Sopenharmony_ciBelow shows how the grid looks when dragging of grid items starts. 1246e41f4b71Sopenharmony_ci 1247e41f4b71Sopenharmony_ci 1248e41f4b71Sopenharmony_ci 1249e41f4b71Sopenharmony_ciBelow shows how the grid looks when dragging of grid items is in progress. 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ci 1252e41f4b71Sopenharmony_ci 1253e41f4b71Sopenharmony_ciBelow shows how the grid looks after grid item 1 and grid item 6 swap their positions. 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci 1256e41f4b71Sopenharmony_ci 1257e41f4b71Sopenharmony_ci### Example 6 1258e41f4b71Sopenharmony_ci 1259e41f4b71Sopenharmony_ciExample of using **layoutDirection**, **maxcount**, **minCount**, and **cellLength**: 1260e41f4b71Sopenharmony_ci 1261e41f4b71Sopenharmony_ci```ts 1262e41f4b71Sopenharmony_ci@Entry 1263e41f4b71Sopenharmony_ci@Component 1264e41f4b71Sopenharmony_cistruct GridExample { 1265e41f4b71Sopenharmony_ci @State numbers: string[] = [] 1266e41f4b71Sopenharmony_ci 1267e41f4b71Sopenharmony_ci aboutToAppear() { 1268e41f4b71Sopenharmony_ci for (let i = 1; i <= 30; i++) { 1269e41f4b71Sopenharmony_ci this.numbers.push(i + '') 1270e41f4b71Sopenharmony_ci } 1271e41f4b71Sopenharmony_ci } 1272e41f4b71Sopenharmony_ci 1273e41f4b71Sopenharmony_ci build() { 1274e41f4b71Sopenharmony_ci Scroll() { 1275e41f4b71Sopenharmony_ci Column({ space: 5 }) { 1276e41f4b71Sopenharmony_ci Blank() 1277e41f4b71Sopenharmony_ci Text ('The layoutDirection, maxcount, minCount, and cellLength parameters take effect only when neither rowsTemplate nor columnsTemplate is set.') 1278e41f4b71Sopenharmony_ci .fontSize(15).fontColor(0xCCCCCC).width('90%') 1279e41f4b71Sopenharmony_ci Grid() { 1280e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 1281e41f4b71Sopenharmony_ci GridItem() { 1282e41f4b71Sopenharmony_ci Text(day).fontSize(16).backgroundColor(0xF9CF93) 1283e41f4b71Sopenharmony_ci }.width(40).height(80).borderWidth(2).borderColor(Color.Red) 1284e41f4b71Sopenharmony_ci }, (day: string) => day) 1285e41f4b71Sopenharmony_ci } 1286e41f4b71Sopenharmony_ci .height(300) 1287e41f4b71Sopenharmony_ci .columnsGap(10) 1288e41f4b71Sopenharmony_ci .rowsGap(10) 1289e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 1290e41f4b71Sopenharmony_ci .maxCount(6) 1291e41f4b71Sopenharmony_ci .minCount(2) 1292e41f4b71Sopenharmony_ci .cellLength(0) 1293e41f4b71Sopenharmony_ci .layoutDirection(GridDirection.Row) 1294e41f4b71Sopenharmony_ci } 1295e41f4b71Sopenharmony_ci .width('90%').margin({ top: 5, left: 5, right: 5 }) 1296e41f4b71Sopenharmony_ci .align(Alignment.Center) 1297e41f4b71Sopenharmony_ci } 1298e41f4b71Sopenharmony_ci } 1299e41f4b71Sopenharmony_ci} 1300e41f4b71Sopenharmony_ci``` 1301e41f4b71Sopenharmony_ci 1302e41f4b71Sopenharmony_ci 1303e41f4b71Sopenharmony_ci 1304e41f4b71Sopenharmony_ci### Example 7 1305e41f4b71Sopenharmony_ci 1306e41f4b71Sopenharmony_ciThis example demonstrates how to adjust the number of columns in the grid with a pinch gesture using two fingers. 1307e41f4b71Sopenharmony_ci 1308e41f4b71Sopenharmony_ci```ts 1309e41f4b71Sopenharmony_ci// xxx.ets 1310e41f4b71Sopenharmony_ci@Entry 1311e41f4b71Sopenharmony_ci@Component 1312e41f4b71Sopenharmony_cistruct GridExample { 1313e41f4b71Sopenharmony_ci @State numbers: String[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'] 1314e41f4b71Sopenharmony_ci @State columns: number = 2 1315e41f4b71Sopenharmony_ci 1316e41f4b71Sopenharmony_ci aboutToAppear() { 1317e41f4b71Sopenharmony_ci let lastCount = AppStorage.get<number>('columnsCount') 1318e41f4b71Sopenharmony_ci if (typeof lastCount != 'undefined') { 1319e41f4b71Sopenharmony_ci this.columns = lastCount 1320e41f4b71Sopenharmony_ci } 1321e41f4b71Sopenharmony_ci } 1322e41f4b71Sopenharmony_ci 1323e41f4b71Sopenharmony_ci build() { 1324e41f4b71Sopenharmony_ci Column({ space: 5 }) { 1325e41f4b71Sopenharmony_ci Row() { 1326e41f4b71Sopenharmony_ci Text('Pinch to change the number of columns') 1327e41f4b71Sopenharmony_ci .height('5%') 1328e41f4b71Sopenharmony_ci .margin({ top: 10, left: 20 }) 1329e41f4b71Sopenharmony_ci } 1330e41f4b71Sopenharmony_ci 1331e41f4b71Sopenharmony_ci Grid() { 1332e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 1333e41f4b71Sopenharmony_ci ForEach(this.numbers, (day: string) => { 1334e41f4b71Sopenharmony_ci GridItem() { 1335e41f4b71Sopenharmony_ci Text(day) 1336e41f4b71Sopenharmony_ci .fontSize(16) 1337e41f4b71Sopenharmony_ci .backgroundColor(0xF9CF93) 1338e41f4b71Sopenharmony_ci .width('100%') 1339e41f4b71Sopenharmony_ci .height(80) 1340e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 1341e41f4b71Sopenharmony_ci } 1342e41f4b71Sopenharmony_ci }, (day: string) => day) 1343e41f4b71Sopenharmony_ci }, (day: string) => day) 1344e41f4b71Sopenharmony_ci } 1345e41f4b71Sopenharmony_ci .columnsTemplate('1fr '.repeat(this.columns)) 1346e41f4b71Sopenharmony_ci .columnsGap(10) 1347e41f4b71Sopenharmony_ci .rowsGap(10) 1348e41f4b71Sopenharmony_ci .width('90%') 1349e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 1350e41f4b71Sopenharmony_ci .backgroundColor(0xFAEEE0) 1351e41f4b71Sopenharmony_ci .height('100%') 1352e41f4b71Sopenharmony_ci .cachedCount(3) 1353e41f4b71Sopenharmony_ci // Switching the number of columns triggers a reordering animation for the item positions. 1354e41f4b71Sopenharmony_ci .animation({ 1355e41f4b71Sopenharmony_ci duration: 300, 1356e41f4b71Sopenharmony_ci curve: Curve.Smooth 1357e41f4b71Sopenharmony_ci }) 1358e41f4b71Sopenharmony_ci .priorityGesture( 1359e41f4b71Sopenharmony_ci PinchGesture() 1360e41f4b71Sopenharmony_ci .onActionEnd((event: GestureEvent) => { 1361e41f4b71Sopenharmony_ci console.info('end scale:' + event.scale) 1362e41f4b71Sopenharmony_ci // When a user performs a pinch-to-zoom gesture by moving their fingers apart, and the number of columns decreases to a certain threshold (in this case, 2), it will cause the items to enlarge. 1363e41f4b71Sopenharmony_ci if (event.scale > 2) { 1364e41f4b71Sopenharmony_ci this.columns-- 1365e41f4b71Sopenharmony_ci } else if (event.scale < 0.6) { 1366e41f4b71Sopenharmony_ci this.columns++ 1367e41f4b71Sopenharmony_ci } 1368e41f4b71Sopenharmony_ci // You can set the maximum and minimum number of columns based on the device screen width. Here, the minimum number of columns is 1, and the maximum number of columns is 4. 1369e41f4b71Sopenharmony_ci this.columns = Math.min(4, Math.max(1, this.columns)); 1370e41f4b71Sopenharmony_ci AppStorage.setOrCreate<number>('columnsCount', this.columns) 1371e41f4b71Sopenharmony_ci }) 1372e41f4b71Sopenharmony_ci ) 1373e41f4b71Sopenharmony_ci }.width('100%').margin({ top: 5 }) 1374e41f4b71Sopenharmony_ci } 1375e41f4b71Sopenharmony_ci} 1376e41f4b71Sopenharmony_ci``` 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ci 1379e41f4b71Sopenharmony_ci 1380e41f4b71Sopenharmony_ci### Example 8 1381e41f4b71Sopenharmony_ciThis example shows the usage of **auto-fill**, **auto-fit**, and **auto-stretch** in the [columnsTemplate](#columnstemplate) attribute. 1382e41f4b71Sopenharmony_ci 1383e41f4b71Sopenharmony_ci```ts 1384e41f4b71Sopenharmony_ci@Entry 1385e41f4b71Sopenharmony_ci@Component 1386e41f4b71Sopenharmony_cistruct GridColumnsTemplate { 1387e41f4b71Sopenharmony_ci data: number[] = [0, 1, 2, 3, 4, 5] 1388e41f4b71Sopenharmony_ci data1: number[] = [0, 1, 2, 3, 4, 5] 1389e41f4b71Sopenharmony_ci data2: number[] = [0, 1, 2, 3, 4, 5] 1390e41f4b71Sopenharmony_ci 1391e41f4b71Sopenharmony_ci build() { 1392e41f4b71Sopenharmony_ci Column({ space: 10 }) { 1393e41f4b71Sopenharmony_ci Text('auto-fill auto-calculates the number of columns based on the set column width').width('90%') 1394e41f4b71Sopenharmony_ci Grid() { 1395e41f4b71Sopenharmony_ci ForEach(this.data, (item: number) => { 1396e41f4b71Sopenharmony_ci GridItem() { 1397e41f4b71Sopenharmony_ci Text('N' + item).height(80) 1398e41f4b71Sopenharmony_ci } 1399e41f4b71Sopenharmony_ci .backgroundColor(Color.Orange) 1400e41f4b71Sopenharmony_ci }) 1401e41f4b71Sopenharmony_ci } 1402e41f4b71Sopenharmony_ci .width('90%') 1403e41f4b71Sopenharmony_ci .border({ width: 1, color: Color.Black }) 1404e41f4b71Sopenharmony_ci .columnsTemplate('repeat(auto-fill, 70)') 1405e41f4b71Sopenharmony_ci .columnsGap(10) 1406e41f4b71Sopenharmony_ci .rowsGap(10) 1407e41f4b71Sopenharmony_ci .height(150) 1408e41f4b71Sopenharmony_ci 1409e41f4b71Sopenharmony_ci Text('auto-fit calculates the number of columns based on the specified column width, and then any remaining space is evenly distributed across all columns').width('90%') 1410e41f4b71Sopenharmony_ci Grid() { 1411e41f4b71Sopenharmony_ci ForEach(this.data1, (item: number) => { 1412e41f4b71Sopenharmony_ci GridItem() { 1413e41f4b71Sopenharmony_ci Text('N' + item).height(80) 1414e41f4b71Sopenharmony_ci } 1415e41f4b71Sopenharmony_ci .backgroundColor(Color.Orange) 1416e41f4b71Sopenharmony_ci }) 1417e41f4b71Sopenharmony_ci } 1418e41f4b71Sopenharmony_ci .width('90%') 1419e41f4b71Sopenharmony_ci .border({ width: 1, color: Color.Black }) 1420e41f4b71Sopenharmony_ci .columnsTemplate('repeat(auto-fit, 70)') 1421e41f4b71Sopenharmony_ci .columnsGap(10) 1422e41f4b71Sopenharmony_ci .rowsGap(10) 1423e41f4b71Sopenharmony_ci .height(150) 1424e41f4b71Sopenharmony_ci 1425e41f4b71Sopenharmony_ci Text('auto-stretch calculates the number of columns based on the specified column width, and then any remaining space is evenly distributed into the gaps between columns').width('90%') 1426e41f4b71Sopenharmony_ci Grid() { 1427e41f4b71Sopenharmony_ci ForEach(this.data2, (item: number) => { 1428e41f4b71Sopenharmony_ci GridItem() { 1429e41f4b71Sopenharmony_ci Text('N' + item).height(80) 1430e41f4b71Sopenharmony_ci } 1431e41f4b71Sopenharmony_ci .backgroundColor(Color.Orange) 1432e41f4b71Sopenharmony_ci }) 1433e41f4b71Sopenharmony_ci } 1434e41f4b71Sopenharmony_ci .width('90%') 1435e41f4b71Sopenharmony_ci .border({ width: 1, color: Color.Black }) 1436e41f4b71Sopenharmony_ci .columnsTemplate('repeat(auto-stretch, 70)') 1437e41f4b71Sopenharmony_ci .columnsGap(10) 1438e41f4b71Sopenharmony_ci .rowsGap(10) 1439e41f4b71Sopenharmony_ci .height(150) 1440e41f4b71Sopenharmony_ci } 1441e41f4b71Sopenharmony_ci .width('100%') 1442e41f4b71Sopenharmony_ci .height('100%') 1443e41f4b71Sopenharmony_ci } 1444e41f4b71Sopenharmony_ci} 1445e41f4b71Sopenharmony_ci``` 1446e41f4b71Sopenharmony_ci 1447e41f4b71Sopenharmony_ci 1448e41f4b71Sopenharmony_ci 1449e41f4b71Sopenharmony_ci### Example 9 1450e41f4b71Sopenharmony_ciThis example implements a grid that contains two columns. The grid item in each column consists of two **Column** components with determined heights and one **Text** component with an undetermined height. 1451e41f4b71Sopenharmony_ci 1452e41f4b71Sopenharmony_ciBy default, the heights of the left and right grid items may differ; however, after the grid's **alignItems** attribute is set to **GridItemAlignment.STRETCH**, the grid item with a shorter height in a row will adopt the height of the taller grid item, aligning their heights within the same row. 1453e41f4b71Sopenharmony_ci 1454e41f4b71Sopenharmony_ci```ts 1455e41f4b71Sopenharmony_ci@Entry 1456e41f4b71Sopenharmony_ci@Component 1457e41f4b71Sopenharmony_cistruct Index { 1458e41f4b71Sopenharmony_ci @State data: number[] = []; 1459e41f4b71Sopenharmony_ci @State items: number[] = []; 1460e41f4b71Sopenharmony_ci 1461e41f4b71Sopenharmony_ci aboutToAppear(): void { 1462e41f4b71Sopenharmony_ci for (let i = 0; i < 100; i++) { 1463e41f4b71Sopenharmony_ci this.data.push(i) 1464e41f4b71Sopenharmony_ci this.items.push(this.getSize()) 1465e41f4b71Sopenharmony_ci } 1466e41f4b71Sopenharmony_ci } 1467e41f4b71Sopenharmony_ci 1468e41f4b71Sopenharmony_ci getSize() { 1469e41f4b71Sopenharmony_ci let ret = Math.floor(Math.random() * 5) 1470e41f4b71Sopenharmony_ci return Math.max(1, ret) 1471e41f4b71Sopenharmony_ci } 1472e41f4b71Sopenharmony_ci 1473e41f4b71Sopenharmony_ci build() { 1474e41f4b71Sopenharmony_ci Column({ space: 10 }) { 1475e41f4b71Sopenharmony_ci Text('Grid alignItems sample code') 1476e41f4b71Sopenharmony_ci 1477e41f4b71Sopenharmony_ci Grid() { 1478e41f4b71Sopenharmony_ci ForEach(this.data, (item: number) => { 1479e41f4b71Sopenharmony_ci // GridItem and Column components, when left without explicitly set heights, will by default adapt to the size of their child components. With alignItems set to STRETCH, they will instead take on the height of the tallest component in the current row. 1480e41f4b71Sopenharmony_ci // If the height is explicitly set, the component maintains the defined height and will not follow the height of the tallest component in the current row. 1481e41f4b71Sopenharmony_ci GridItem() { 1482e41f4b71Sopenharmony_ci Column() { 1483e41f4b71Sopenharmony_ci Column().height(100).backgroundColor('#D5D5D5').width('100%') 1484e41f4b71Sopenharmony_ci // The Text component in the center is set with flexGrow(1) to automatically fill the available space within the parent component. 1485e41f4b71Sopenharmony_ci Text('A piece of text.'.repeat(this.items[item])) 1486e41f4b71Sopenharmony_ci .flexGrow(1).width('100%').align(Alignment.TopStart) 1487e41f4b71Sopenharmony_ci .backgroundColor('#F7F7F7') 1488e41f4b71Sopenharmony_ci Column().height(50).backgroundColor('#707070').width('100%') 1489e41f4b71Sopenharmony_ci } 1490e41f4b71Sopenharmony_ci } 1491e41f4b71Sopenharmony_ci .border({ color: Color.Black, width: 1 }) 1492e41f4b71Sopenharmony_ci }) 1493e41f4b71Sopenharmony_ci } 1494e41f4b71Sopenharmony_ci .columnsGap(10) 1495e41f4b71Sopenharmony_ci .rowsGap(5) 1496e41f4b71Sopenharmony_ci .columnsTemplate('1fr 1fr') 1497e41f4b71Sopenharmony_ci .width('80%') 1498e41f4b71Sopenharmony_ci .height('100%') 1499e41f4b71Sopenharmony_ci // When the grid has its alignItems attribute set to STRETCH, it adjusts the height of all grid items in a row to match the height of the tallest grid item in that row. 1500e41f4b71Sopenharmony_ci .alignItems(GridItemAlignment.STRETCH) 1501e41f4b71Sopenharmony_ci .scrollBar(BarState.Off) 1502e41f4b71Sopenharmony_ci } 1503e41f4b71Sopenharmony_ci .height('100%') 1504e41f4b71Sopenharmony_ci .width('100%') 1505e41f4b71Sopenharmony_ci } 1506e41f4b71Sopenharmony_ci} 1507e41f4b71Sopenharmony_ci 1508e41f4b71Sopenharmony_ci``` 1509e41f4b71Sopenharmony_ci 1510