Last active
February 19, 2024 17:10
-
-
Save darnmason/38a1a5178a06470202784050f4dc1cdf to your computer and use it in GitHub Desktop.
A custom Android ViewGroup that contains a single child and allows you to swipe it to the left, with a callback once the swipe is complete. For example to dismiss a view. Works well in a LinearLayout with animateLayoutChanges=true when setting the SwipeView visibility to GONE on swipe.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.support.v4.view.ViewCompat | |
import android.support.v4.widget.ViewDragHelper | |
import android.util.AttributeSet | |
import android.view.MotionEvent | |
import android.view.View | |
import android.widget.FrameLayout | |
class SwipeView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) { | |
fun setOnSwipeListener(listener: () -> Unit) { | |
swipeCallback = listener | |
} | |
fun destroy() { | |
dragHelper.abort() | |
swipeCallback = null | |
} | |
companion object { | |
private const val AUTO_SWIPE_VELOCITY = 800.0 | |
} | |
private var previousState = ViewDragHelper.STATE_IDLE | |
private var draggingLeft = false | |
private var draggedThreshold = false | |
private var swipeComplete = false | |
private var swipeCallback: (() -> Unit)? = null | |
private val dragHelperCallback = object : ViewDragHelper.Callback() { | |
override fun tryCaptureView(child: View, pointerId: Int) = child === getSlidingView() | |
override fun getViewHorizontalDragRange(child: View) = width | |
override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int) = Math.min(left, 0) | |
override fun onViewPositionChanged(changedView: View, left: Int, top: Int, dx: Int, dy: Int) { | |
draggingLeft = Math.abs(left) > width / 20 && dx <= 0 | |
draggedThreshold = Math.abs(left) > width / 2 | |
} | |
override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) { | |
if (Math.abs(xvel) > AUTO_SWIPE_VELOCITY) { | |
swipeComplete = true | |
dragHelper.flingCapturedView(-width, 0, -width, 0) | |
ViewCompat.postInvalidateOnAnimation(this@SwipeView) | |
} else { | |
var finalLeft = 0 | |
if (draggedThreshold) { | |
finalLeft = -width | |
swipeComplete = true | |
} | |
if (dragHelper.settleCapturedViewAt(finalLeft, 0)) { | |
ViewCompat.postInvalidateOnAnimation(this@SwipeView) | |
} | |
} | |
} | |
override fun onViewDragStateChanged(state: Int) { | |
if (state != previousState && state == ViewDragHelper.STATE_IDLE && swipeComplete) { | |
getSlidingView()?.post(swipeCallback) | |
} | |
previousState = state | |
} | |
} | |
private val dragHelper: ViewDragHelper = ViewDragHelper.create(this, 1f, dragHelperCallback) | |
private fun getSlidingView(): View? { | |
return getChildAt(0) | |
} | |
override fun onTouchEvent(event: MotionEvent?): Boolean { | |
event?.let { ev -> | |
dragHelper.processTouchEvent(ev) | |
parent?.let { | |
when (ev.actionMasked) { | |
MotionEvent.ACTION_MOVE -> { | |
parent.requestDisallowInterceptTouchEvent(draggingLeft) | |
} | |
MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> { | |
draggingLeft = false | |
parent.requestDisallowInterceptTouchEvent(false) | |
} | |
} | |
} | |
return true | |
} | |
return super.onTouchEvent(event) | |
} | |
override fun onInterceptTouchEvent(event: MotionEvent?): Boolean { | |
return event?.takeIf { dragHelper.shouldInterceptTouchEvent(event) } != null || super.onInterceptTouchEvent(event) | |
} | |
override fun computeScroll() { | |
if (dragHelper.continueSettling(true)) { | |
ViewCompat.postInvalidateOnAnimation(this) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be great if there will an option to select direction (left, right). Also, it looks like this code will activate swipeCallback even if swiped to the right, if X is large enough.