Swipe Android Library

repository·RxJava2.x·Indexed 43 days ago

https://github.com/pwittchen/swipe

An Android library for detecting swipe gestures, offering both an imperative API using SwipeListener and a reactive API using RxJava Observables. It supports configurable sensitivity thresholds for swiping and swiped events and provides a SwipeEvent enum for reactive event handling.

Tokens
1.5K
Snippets
5
Records
5
Agent score
6%

What's inside Swipe

  1. Use the reactive RxJava API

    RxJava2.x

    To detect swipe events using RxJava, follow these steps:

    1. Declare a Swipe instance and a Disposable in your Activity.
    2. Initialize Swipe and subscribe to its observe() method. The observable emits SwipeEvent enum values.
    3. Override dispatchTouchEvent(MotionEvent event) to pass touch events to the Swipe instance.
    4. Dispose of the Disposable in onPause() to prevent memory leaks.

    Available SwipeEvent values:

    • SWIPING_LEFT / SWIPED_LEFT
    • SWIPING_RIGHT / SWIPED_RIGHT
    • SWIPING_UP / SWIPED_UP
    • SWIPING_DOWN / SWIPED_DOWN
    // Step 1: Declare
    private Swipe swipe;
    private Disposable disposable;
    
    // Step 2: Initialize and subscribe
    @Override protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    
      swipe = new Swipe();
      disposable = swipe.observe()
          .subscribeOn(Schedulers.computation())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(swipeEvent -> info.setText(swipeEvent.toString()));
    }
    
    // Step 3: Pass events
    @Override public boolean dispatchTouchEvent(MotionEvent event) {
      swipe.dispatchTouchEvent(event);
      return super.dispatchTouchEvent(event);
    }
    
    // Step 4: Dispose
    @Override protected void onPause() {
      super.onPause();
      if (disposable != null && !disposable.isDisposed()) {
        disposable.dispose();
      }
    }
  2. Use the imperative SwipeListener API

    RxJava2.x

    To detect swipe events using a listener, follow these steps:

    1. Declare a Swipe instance in your Activity.
    2. Initialize Swipe and provide a SwipeListener implementation. The listener provides callbacks for both the active swiping state (onSwiping...) and the completed swipe state (onSwiped...).
    3. Override dispatchTouchEvent(MotionEvent event) in your Activity to pass touch events to the Swipe instance.

    If you only need to react to a subset of events, extend SimpleSwipeListener instead of implementing the full SwipeListener interface.

    // Step 1: Declare
    private Swipe swipe;
    
    // Step 2: Initialize and set listener
    @Override protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
    
      swipe = new Swipe();
      swipe.setListener(new SwipeListener() {
        @Override public void onSwipingLeft(final MotionEvent event) { /* ... */ }
        @Override public void onSwipedLeft(final MotionEvent event) { /* ... */ }
        // ... other methods
      });
    }
    
    // Step 3: Pass events
    @Override public boolean dispatchTouchEvent(MotionEvent event) {
      swipe.dispatchTouchEvent(event);
      return super.dispatchTouchEvent(event);
    }
  3. Install the swipe library

    RxJava2.x

    You can add the library to your Android project using Maven or Gradle. Note that the artifact ID differs depending on whether you want the RxJava 1.x or RxJava 2.x version.

    Maven (RxJava 2.x):

    <dependency>
        <groupId>com.github.pwittchen</groupId>
        <artifactId>swipe-rx2</artifactId>
        <version>0.3.0</version>
    </dependency>

    Gradle (RxJava 2.x):

    dependencies {
      compile 'com.github.pwittchen:swipe-rx2:0.3.0'
    }
    <!-- Maven -->
    <dependency>
        <groupId>com.github.pwittchen</groupId>
        <artifactId>swipe-rx2</artifactId>
        <version>0.3.0</version>
    </dependency>
    
    <!-- Gradle -->
    dependencies {
      compile 'com.github.pwittchen:swipe-rx2:0.3.0'
    }
  4. Configure swipe sensitivity thresholds

    RxJava2.x

    You can adjust the sensitivity of swipe detection by using the Swipe constructor that accepts threshold values:

    Swipe(int swipingThreshold, int swipedThreshold)

    • swipingThreshold: The distance required to trigger a SWIPING_... event. Default is 20.
    • swipedThreshold: The distance required to trigger a SWIPED_... event. Default is 100.

    Decreasing these values increases the sensitivity of the swipe detection.

    // Example of custom thresholds
    swipe = new Swipe(10, 50); // More sensitive than defaults
  5. Reference the SwipeEvent enum values

    RxJava2.x

    When using the RxJava reactive API, the observe() method emits instances of the SwipeEvent enum. These values represent the current state of the gesture:

    public enum SwipeEvent {
      SWIPING_LEFT,
      SWIPED_LEFT,
      SWIPING_RIGHT,
      SWIPED_RIGHT,
      SWIPING_UP,
      SWIPED_UP,
      SWIPING_DOWN,
      SWIPED_DOWN
    }
    public enum SwipeEvent {
      SWIPING_LEFT,
      SWIPED_LEFT,
      SWIPING_RIGHT,
      SWIPED_RIGHT,
      SWIPING_UP,
      SWIPED_UP,
      SWIPING_DOWN,
      SWIPED_DOWN
    }