Correctly exchange text using Textswitcher

Asked

Viewed 44 times

2

I need to use a component TextSwitcher with the intention that when the user moves this TextSwitcher to the right as well as to the left the text of the same.

For this I created a variable of type String[] and stored 5 values, and used the event OnTouch on the type component TextSwitcher, but with the following code I will post below the text is not exchanged, remains the same, until give an error of ArrayOfBounds because I didn’t handle the size of the Strings array.

What would be the best way to exchange texts?

    tsIntroduction.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getActionMasked()) {

            case MotionEvent.ACTION_DOWN:
                // Variaveis declaradas no topo da aplicação
                initialX = event.getX();
                initialY = event.getY();

            case MotionEvent.ACTION_UP:
                float finalX = event.getX();
                float finalY = event.getY();

                if (initialX < finalX) {
                    pos =+ 1;
                }

                if (initialX > finalX) {
                    pos =- 1;
                }
            }

            // Varíavel pos declarada no inicio da aplicação para pegar a posição atual
            tsIntroduction.setText(descriptions[pos]);
            return true;
        }

1 answer

1


I managed to solve the problem, the following was done:

I implemented the interface OnGestureListener package android.view.GestureDetector and used two main methods to make the logic:

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  this.mDetector.onTouchEvent(event);
  return super.onTouchEvent(event);
 }

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
     float sensitvity = 50;

      if((e1.getX() - e2.getX()) > sensitvity){
          pos += 1;
          tsIntroduction.setText(descriptions[pos]);
      }else if((e2.getX() - e1.getX()) > sensitvity){
          pos -= 1;
          tsIntroduction.setText(descriptions[pos]);
      }

      return true;
}

Where the variable mDetector is the type GestureDetectorCompat and initializes as follows:

mDetector = new GestureDetectorCompat(this,this);

And tsIntroduction would be the ImageSwitcher and descriptions is the string array I created.

I didn’t implement the logic to treat the error ArrayOutOfBounds, but it’s not hard to create.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.