Blog Infos
Author
Published
Topics
,
Published
enum class SwipeDirection {
ALL, // swipe allowed in left and right both directions
LEFT, // swipe allowed in only Left direction
RIGHT, // only right
NONE // swipe is disabled completely
}

This enum enables us to specify directions in which a user is allowed to swipe at any given point in time. Now we need to create a function that takes MotionEvent object and attempts to detect the direction user is trying to swipe. It should then compare it with the currently set SwipeDirectionand return true if the user’s intention and current setting match or falseotherwise. Here is that function

private fun isSwipeAllowed(event: MotionEvent): Boolean {
if (direction === SwipeDirection.ALL) return true
if (direction == SwipeDirection.NONE) //disable any swipe
return false
if (event.action == MotionEvent.ACTION_DOWN) {
initialXValue = event.x
return true
}
if (event.action == MotionEvent.ACTION_MOVE) {
try {
val diffX: Float = event.x - initialXValue
if (diffX > 0 && direction == SwipeDirection.RIGHT) {
// swipe from left to right detected
return false
} else if (diffX < 0 && direction == SwipeDirection.LEFT) {
// swipe from right to left detected
return false
}
} catch (exception: Exception) {
exception.printStackTrace()
}
}
return true
}
swipeControlTouchListener.setSwipeDirection(SwipeDirection.LEFT)
view raw SetSwipeDir.kt hosted with ❤ by GitHub

Job Offers

Job Offers


    Android Developer

    Small and Modern GmbH
    Hamburg, Remote (Germany)
    • Full Time
    apply now

    Senior Android Engineer, Infrastructure

    Peloton
    New York, USA
    • Full Time
    apply now

    Senior Android Engineer – BILD (m/f/d)

    Axel Springer National Media & Tech
    Berlin
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
Google

Jobs

Now here is a little hacky part where I had to get access to ViewPager2 to RecyclerView in a questionable way.

// apply touch listener on ViewPager RecyclerView
val recyclerView = binding.viewPager[0] as? RecyclerView
if (recyclerView != null) {
recyclerView.addOnItemTouchListener(swipeControlTouchListener)
} else {
Log.w(localClassName, "RecyclerView is null, API/Version changed ?!")
}

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
Nowadays authentication has become common in almost all apps. And many of us know…
READ MORE
blog
Collections are a set of interfaces and classes that implement highly optimised data structures.…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu