1

I am writing a custom component in HarmonyOS using Java SDK and it is a custom page slider indicator. To do that I have added a PageChangedListener which provides three override methods.

public class CustomPageSliderIndicator extends Component implements PageSlider.PageChangedListener{

@Override
public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {}

@Override
public void onPageChosen(int i) {}

@Override
public void onPageSlideStateChanged(int i) { }
}

Whenever the user slides a page, onPageSliding will be called, here I am facing the problem that the position and positionOffset are the same for sliding right and left.

So, how to know the direction of sliding?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108

2 Answers2

1

Introduce an instance variable (currentPosition) to keep track of the current page.

    private int currentPosition;

...

    pageSlider.addPageChangedListener(new PageSlider.PageChangedListener() {
                    @Override
                    public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
                    }

                    @Override
                    public void onPageSlideStateChanged(int state) {
                    }

                    @Override
                    public void onPageChosen(int position) {
                         if(currentPosition < position) {
                            // handle swipe LEFT
                        } else if(currentPosition > position){
                            // handle swipe RIGHT
                        }
                        currentPosition = position; // Update current position
                    }
                });

Gowtham GS
  • 478
  • 2
  • 5
0

onPageSliding will be called when the user slides a page, and not much to do with PageSliderIndicator.

The values of position and positionOffset in the pageslider are the same. This callback parameter is currently designed as such.

void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels)

If this parameter is set to slide left and right, the third parameter itemPosOffsetPixels shows the direction. Slide to the right is positive, and slide to the left is negative.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • I guess itemPosOffsetPixels gives the position offset rather than the sliding direction. For example, if we start sliding to right it would be positive, at halfway if we decided to retrieve to the original position and in that process also it would be positive whereas the sliding direction would be to left. – YOGESH SANKU Jul 30 '21 at 06:36