Sliding events of Hongmeng learning notes (common actions)

Keywords: Java Front-end IoT harmonyos


Slide event: after clicking, slide this module.

Common scenes: rotation chart sliding, article list up and down linkage, novel page turning, etc.

Common actions of sliding events: Press, move and release

  We learned the click, double click and long press events. Today we will learn a new knowledge point, sliding event, which will be often used in our actual development.

If you don't say much, you can directly operate the code:

The xml code file is as follows:

<?xml version="1.0" encoding="utf-8"?>

        ohos:text="Sliding interface"


The above is our xml file. We found that I set the id for the DirectionalLayout component. This is because when we use the sliding event, we are operating our interface, so we need to set the id for DirectionalLayout, so that we can obtain the DirectionalLayout component for our convenience.

Next is our java code to write our sliding event. The code is as follows:

package com.example.mydemoslide.slice;

import com.example.mydemoslide.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener{
    private Text txt;
    public void onStart(Intent intent) {
        //txt component found
        txt= (Text) findComponentById(ResourceTable.Id_text_helloworld);
        //Find our component that represents sliding. Because we really slide the whole page, we directly find the DirectionalLayout component
        DirectionalLayout view = (DirectionalLayout) findComponentById(ResourceTable.Id_view);
        //Bind sliding events to our components

    public void onActive() {

    public void onForeground(Intent intent) {

    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        //Get our sliding event status
        int touState = touchEvent.getAction();
        //Status of sliding event press
            txt.setText("The finger clicked");
        //Status of sliding event release
        else if(touState==touchEvent.PRIMARY_POINT_UP){
            txt.setText("The finger is loose");
        //State of sliding event movement
        else if(touState==touchEvent.POINT_MOVE){
            txt.setText("The fingers moved");
        return true;

As mentioned above, we first find our component and bind our sliding event to it. Our sliding event gives us not only a component parameter, but also a touchEvent parameter. touchEvent is used to get our sliding event. getAction() can get the status of our current sliding event. The toucheEvent class provides us with many definition properties, including PRIMARY_POINT_DOWN,PRIMARY_POINT_UP and POINT_MOVE is our common attribute,

Press, release and move.

The effects are as follows:

Initial page:    After pressing:    After loosening:    Moving:

  The above is our sliding event implementation effect and code, which requires the click of the source code[ HarmonyOS long click event source code. Rar Java document class resource - CSDN Download ]Download


In a sliding event, return false is executed only once.

return true to get the response of moving and loosening status.


 ================== It's not easy to share. I've seen it here. I don't like the collection===================

Posted by Jeb. on Tue, 30 Nov 2021 09:06:41 -0800