简体   繁体   中英

How to capture swipe up and swipe down actions while using TalkBack for Android

Trying to use the material slider for Android, from the fragment or activity side, can we capture the scroll up or scroll down gesture coming from TalkBack?

May this code help you.

public class MainActivity extends AppCompatActivity {
   OnSwipeTouchListener onSwipeTouchListener;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      onSwipeTouchListener = new OnSwipeTouchListener(this, findViewById(R.id.relativeLayout));
   }
   public static class OnSwipeTouchListener implements View.OnTouchListener {
      private final GestureDetector gestureDetector;
      Context context;
      OnSwipeTouchListener(Context ctx, View mainView) {
         gestureDetector = new GestureDetector(ctx, new GestureListener());
         mainView.setOnTouchListener(this);
         context = ctx;
      }
      @Override
      public boolean onTouch(View v, MotionEvent event) {
         return gestureDetector.onTouchEvent(event);
      }
      public class GestureListener extends
      GestureDetector.SimpleOnGestureListener {
         private static final int SWIPE_THRESHOLD = 100;
         private static final int SWIPE_VELOCITY_THRESHOLD = 100;
         @Override
         public boolean onDown(MotionEvent e) {
            return true;
         }
         @Override
         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
               float diffY = e2.getY() - e1.getY();
               float diffX = e2.getX() - e1.getX();
               if (Math.abs(diffX) > Math.abs(diffY)) {
                  if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                     if (diffX > 0) {
                        onSwipeRight();
                     } else {
                        onSwipeLeft();
                     }
                     result = true;
                  }
               }
               else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                  if (diffY > 0) {
                     onSwipeBottom();
                  } else {
                     onSwipeTop();
                  }
                  result = true;
               }
            }
            catch (Exception exception) {
               exception.printStackTrace();
            }
            return result;
         }
      }
      void onSwipeRight() {
         Toast.makeText(context, "Swiped Right", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeRight();
      }
      void onSwipeLeft() {
         Toast.makeText(context, "Swiped Left", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeLeft();
      }
      void onSwipeTop() {
         Toast.makeText(context, "Swiped Up", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeTop();
      }
      void onSwipeBottom() {
         Toast.makeText(context, "Swiped Down", Toast.LENGTH_SHORT).show();
         this.onSwipe.swipeBottom();
      }
      interface onSwipeListener {
         void swipeRight();
         void swipeTop();
         void swipeBottom();
         void swipeLeft();
      }
      onSwipeListener onSwipe;
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM