DEV Community

Cover image for The PageSlider sliding component is used
liu yang
liu yang

Posted on • Edited on

The PageSlider sliding component is used

PageSlider Component Usage

The PageSlider component is a powerful tool in HarmonyOS development, designed to create a smooth and intuitive sliding interface between multiple pages. It is particularly useful for creating user interfaces that require horizontal navigation, such as image galleries, multi-page forms, or onboarding screens. In this detailed guide, we will explore how to implement and customize the PageSlider component, covering its basic usage, event handling, and advanced configurations.

Basic Usage of PageSlider

To get started with the PageSlider component, you need to follow these steps:

  1. Import Necessary Modules: First, import the required modules from the HarmonyOS SDK.
  2. Create a PageSlider Instance: Initialize a PageSlider instance.
  3. Define Pages: Define the content and styles for each page.
  4. Set Pages to PageSlider: Assign the defined pages to the PageSlider.
  5. Set Initial Page: Set the initial page to be displayed.
  6. Add Event Listeners: Add listeners to handle page change events.
  7. Display the PageSlider: Finally, display the PageSlider on the screen.

Here is a step-by-step example:

import {ohos} from 'ohos';
import {PageSlider} from 'ohos.ui';

// Create a PageSlider instance
const pageSlider = new PageSlider(context);

// Define the pages for the PageSlider
const pages = [
  {
    content: 'Page 1',
    style: {
      backgroundColor: '#FF0000'
    }
  },
  {
    content: 'Page 2',
    style: {
      backgroundColor: '#00FF00'
    }
  },
  {
    content: 'Page 3',
    style: {
      backgroundColor: '#0000FF'
    }
  }
];

// Set the pages for the PageSlider
pageSlider.setPages(pages);

// Set the current page index
pageSlider.setCurrentPage(0);

// Add a listener for page changes
pageSlider.addOnPageChangedListener({
  onPageSelected: (pageIndex) => {
    console.log('Page selected: ' + pageIndex);
  },
  onPageScrollStateChanged: (state) => {
    console.log('Page scroll state changed: ' + state);
  },
  onPageScrolled: (position, positionOffset) => {
    console.log('Page scrolled: ' + position + ', offset: ' + positionOffset);
  }
});

// Show the PageSlider
pageSlider.show();
Enter fullscreen mode Exit fullscreen mode

Detailed Explanation

  1. Importing Modules:
   import {ohos} from 'ohos';
   import {PageSlider} from 'ohos.ui';
Enter fullscreen mode Exit fullscreen mode

These lines import the necessary modules from the HarmonyOS SDK. The ohos module provides access to the core HarmonyOS functionalities, while PageSlider is imported from the ohos.ui module, which contains UI components.

  1. Creating a PageSlider Instance:
   const pageSlider = new PageSlider(context);
Enter fullscreen mode Exit fullscreen mode

Here, a new instance of PageSlider is created. The context parameter is required to provide the component with the necessary runtime environment information.

  1. Defining Pages:
   const pages = [
     {
       content: 'Page 1',
       style: {
         backgroundColor: '#FF0000'
       }
     },
     {
       content: 'Page 2',
       style: {
         backgroundColor: '#00FF00'
       }
     },
     {
       content: 'Page 3',
       style: {
         backgroundColor: '#0000FF'
       }
     }
   ];
Enter fullscreen mode Exit fullscreen mode

Each page is defined as an object with content and style properties. The content property specifies the text or content to be displayed on the page, while the style property defines the visual appearance, such as background color.

  1. Setting Pages:
   pageSlider.setPages(pages);
Enter fullscreen mode Exit fullscreen mode

This method assigns the defined pages to the PageSlider instance, making them available for navigation.

  1. Setting Initial Page:
   pageSlider.setCurrentPage(0);
Enter fullscreen mode Exit fullscreen mode

This sets the initial page to be displayed when the PageSlider is shown. The index 0 corresponds to the first page in the pages array.

  1. Adding Event Listeners:
   pageSlider.addOnPageChangedListener({
     onPageSelected: (pageIndex) => {
       console.log('Page selected: ' + pageIndex);
     },
     onPageScrollStateChanged: (state) => {
       console.log('Page scroll state changed: ' + state);
     },
     onPageScrolled: (position, positionOffset) => {
       console.log('Page scrolled: ' + position + ', offset: ' + positionOffset);
     }
   });
Enter fullscreen mode Exit fullscreen mode

The addOnPageChangedListener method allows you to add a listener that handles various page change events:

  • onPageSelected: Triggered when a new page is selected.
  • onPageScrollStateChanged: Triggered when the scroll state changes (e.g., idle, dragging, settling).
  • onPageScrolled: Triggered during the scrolling process, providing the current position and offset.
  1. Displaying the PageSlider:
   pageSlider.show();
Enter fullscreen mode Exit fullscreen mode

Finally, the show method is called to render the PageSlider on the screen.

Customizing PageSlider

The PageSlider component can be further customized to meet specific requirements. For example, you can add more complex content to each page, such as images, buttons, or other UI components. You can also customize the transition animations and handle user interactions more dynamically.

Here is an example of adding more complex content to each page:

const pages = [
  {
    content: (
      <div>
        <Text>Page 1</Text>
        <Button text="Click Me" onClick={() => console.log('Button clicked on Page 1')} />
      </div>
    ),
    style: {
      backgroundColor: '#FF0000'
    }
  },
  {
    content: (
      <div>
        <Text>Page 2</Text>
        <Image src="path/to/image.jpg" />
      </div>
    ),
    style: {
      backgroundColor: '#00FF00'
    }
  },
  {
    content: (
      <div>
        <Text>Page 3</Text>
        <Video src="path/to/video.mp4" />
      </div>
    ),
    style: {
      backgroundColor: '#0000FF'
    }
  }
];
Enter fullscreen mode Exit fullscreen mode

In this example, each page contains a combination of text, buttons, images, and video components, demonstrating the flexibility of the PageSlider component.

Handling User Interactions

You can also handle user interactions more dynamically by adding event listeners to individual components within each page. For example, you can add click listeners to buttons or touch listeners to images to trigger specific actions.

Top comments (0)