Correctly change text using TextSwitcher

2

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

For this I created a variable of type String[] and stored 5 values, and I used the OnTouch event in the TextSwitcher component, but with the following code that I will post below the text is not changed, even by giving a ArrayOfBounds error because I did not deal in the size of the Strings array.

What would be the best way to switch 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;
        }
    
asked by anonymous 10.08.2015 / 01:48

1 answer

1

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

I implemented the OnGestureListener interface of the android.view.GestureDetector package 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;
}

Since the variable mDetector is of type GestureDetectorCompat and initializes as follows:

mDetector = new GestureDetectorCompat(this,this);

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

I have not implemented logic to handle the error ArrayOutOfBounds , but it is not hard to create.

    
10.08.2015 / 03:31