> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# `<viewpager>`

APISummary
`<viewpager>` is a horizontally swipeable paging component. It is commonly used to build tabbed paging experiences.

## Usage

Use `<viewpager>` together with `<viewpager-item>` to create horizontally paged content.

### Recommended Usage

- Explicitly set the width of `<viewpager>`: **`width: 100%;`**
- Explicitly set the layout direction of `<viewpager>`: **`display: flex; flex-direction: row;`**
- Explicitly constrain the size and shrinking behavior of `<viewpager-item>`: **`width: 100%; flex-shrink: 0;`**
- **The direct children of `<viewpager>` must be `<viewpager-item>`.**


**This is an example below: viewpager**

**Bundle:** `dist/base.lynx.bundle`

```tsx
// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.

import { root, useState } from "@lynx-js/react";

const pages = [
  { title: "Page 1", color: "#FBE2E1" },
  { title: "Page 2", color: "#DCEEFF" },
  { title: "Page 3", color: "#E5F7DF" },
];

const App = () => {
  const [current, setCurrent] = useState<number>(0);

  return (
    <view
      style={{
        width: "100%",
        height: "100%",
        flexDirection: "column",
        padding: "24px",
        backgroundColor: "#FFFFFF",
        boxSizing: "border-box",
      }}
    >
      <text style={{ fontSize: "32px", fontWeight: "700", color: "#1F2329" }}>ViewPager Placeholder</text>
      <text style={{ marginTop: "12px", fontSize: "24px", color: "#4E5969" }}>
        {`Current page: ${current + 1}/${pages.length}`}
      </text>
      <view
        style={{ marginTop: "24px", width: "100%", height: "50%", flex: 1, borderRadius: "24px", overflow: "hidden" }}
      >
        <viewpager
          style={{ width: "100%", height: "100%", display: "flex", flexDirection: "row" }}
          bindchange={(e) => {
            setCurrent(e.detail.index);
          }}
        >
          {pages.map((page) => {
            return (
              <viewpager-item style={{ width: "100%", height: "100%", flexShrink: 0 }}>
                <view
                  key={page.title}
                  style={{
                    width: "100%",
                    height: "100%",
                    justifyContent: "center",
                    alignItems: "center",
                    backgroundColor: page.color,
                  }}
                >
                  <text style={{ fontSize: "48px", fontWeight: "600", color: "#1F2329" }}>{page.title}</text>
                </view>
              </viewpager-item>
            );
          })}
        </viewpager>
      </view>
    </view>
  );
};

root.render(<App />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.7.0



## Attributes

### `android-always-overscroll`

Android
```tsx
// @defaultValue: false
'android-always-overscroll'?: boolean;
```

On Android, this attribute is used to control the interaction behavior when scrolling to the edges. Setting it to true enables the default bounce effect, while setting it to false disables the bounce effect. The default value is false.

### `android-force-can-scroll`

Android
3.0

```tsx
// @defaultValue: false
'android-force-can-scroll'?: boolean;
```

On Android, this attribute is used to control if at start/end of viewpager and set true, gesture can not pass to parent.

### `bounces`

iOS
Desktop
Harmony
```tsx
// @defaultValue: true
bounces?: boolean;
```

Is a spring effect needed. Note that this effect is not available on Android. On PC, only macOS support this feature.

### `enable-scroll`

Android
iOS
Desktop
Harmony
2.17

```tsx
// @defaultValue: true
'enable-scroll'?: boolean;
```

Enable horizontal scroll gesture

### `initial-select-index`

Android
iOS
Desktop
Harmony
2.17

```tsx
// @defaultValue: 0
'initial-select-index'?: number;
```

Select the specified page at initialization, specifically referring to the first time when there are children.

### `ios-gesture-direction`

iOS
```tsx
// @defaultValue: false
'ios-gesture-direction'?: boolean;
```

When enabled, when sliding to the head or tail, it can respond to the horizontal swipe events of the outer container (horizontal UIScrollView)

### `ios-gesture-offset`

iOS
```tsx
// @defaultValue: 0
'ios-gesture-offset'?: number;
```

When the finger touches within \[0, value] from the left edge of the screen, it doesn't respond to the horizontal swipe gesture, but it can directly respond to the iOS right swipe to return gesture. However, it's not applicable to Android.

### `ios-recognized-gesture-class`

iOS
```tsx
'ios-recognized-gesture-class'?: string;
```

UIGestureRecognizer's class name, be used to identify the UIGestureRecognizer which may be recognized simultaneously with viewpager. This property is designed to let an UIPanGesture work together with viewpager.

### `ios-recognized-view-tag`

iOS
```tsx
// @defaultValue: 0
'ios-recognized-view-tag'?: number;
```

UIView's tag, be used to identify the UIView of the UIGestureRecognizer which identified by
`ios-recognized-gesture-class`
. This property is designed to let an UIPanGesture work together with viewpager.

### `keep-item-view`

Android
iOS
```tsx
// @defaultValue: false
'keep-item-view'?: boolean;
```

Whether to enable the lazy load mode based on early exposure, it needs to be used in conjunction with the lazyComponent.

## Events

Frontend can bind corresponding event callbacks to listen for runtime behaviors of the element, as shown below.

### `bindchange`

Android
iOS
Desktop
Harmony
```tsx
bindchange = (e: ViewPagerChangeEvent) => {};
```

| Field     | Type    | Optional | Default | Platforms                                                                                                                                                                                                                                  | Since | Description                                           |
| --------- | ------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- | ----------------------------------------------------- |
| index     | number  | No       | –       | Android iOS Desktop Harmony                                                |       | Current index                                         |
| isDragged | boolean | No       | –       | Android iOS Desktop Harmony 2.18 | 2.18  | If this change event is caused by the user's dragging |

Page switch event, it will only be triggered when the UI completely switches to the next page (from 100% to 200%).

### `bindoffsetchange`

Android
iOS
Desktop
Harmony
```tsx
bindoffsetchange = (e: ViewPagerOffsetChangeEvent) => {};
```

| Field  | Type   | Optional | Default | Platforms                                                                                                                                                                                   | Since | Description          |
| ------ | ------ | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- | -------------------- |
| offset | number | No       | –       | Android iOS Desktop Harmony |       | The scrolling offset |

Page switch progress callback, the range is the index of each page, for example, from page 0 to page 1, it's 0~~1, from the first page to the second page, it's 1~~2.

### `bindwillchange`

Android
iOS
Desktop
Harmony
2.17

```tsx
bindwillchange = (e: ViewPagerWillChangeEvent) => {};
```

| Field     | Type    | Optional | Default | Platforms                                                                                                                                                                                                                                  | Since | Description                                           |
| --------- | ------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- | ----------------------------------------------------- |
| index     | number  | No       | –       | Android iOS Desktop Harmony                                                |       | Current index                                         |
| isDragged | boolean | No       | –       | Android iOS Desktop Harmony 2.18 | 2.18  | If this change event is caused by the user's dragging |

Page switch event, it will be triggered when the page is about to switch, and at this time the UI has not switched to the next page yet, so the offset is still in the range of 0\~100% (from page 0 to page 1) or 100%\~200% (from page 1 to page 2).

## Methods

Frontend can invoke component methods via the [SelectorQuery](/next/api/lynx-api/nodes-ref/nodes-ref-invoke.md) API.

### `selectTab`

Android
iOS
Desktop
Harmony
```ts

lynx.createSelectorQuery()
     .select('#id')
     .invoke({
      method: 'selectTab',
      params: {
        /**
         * The index to be scrolled to.
         * @Android
         * @iOS
         * @Harmony
         * @PC
         */
        index: 0,
        /**
         * If an animation effect is needed. The default setting is true.
         * @Android
         * @iOS
         * @Harmony
         * @PC
         */
        smooth: true,
      },
      success: function (res) {},
      fail: function (res) {},
    })
    .exec();

```

Slide to the specified page.

```
```

## Compatibility


**Compatibility Table**
**Query:** `elements.viewpager`

**Platform Support**

| Platform | Version Added | Notes |
|----------|---------------|-------|
| Android | 3.9 | - |
| iOS | 3.9 | - |
| HarmonyOS | 3.9 | - |
| Clay Android | 3.9 | - |
| Clay iOS | 3.9 | - |
| Clay Windows | 3.9 | - |
| Clay macOS | 3.9 | - |
| Web | ✅ Yes | - |

**Description:** viewpager

