VerticalSwipeBehavior
VerticalSwipeBehavior is a small and flexible vertical swipe behavior for android CoordinatorLayout. Moves the released view at the given vertical position.
Download
Check repository
allprojects {
repositories {
jcenter()
}
}
Add dependency in to build.gradle
dependencies {
implementation 'io.apiqa.android:verticalswipebehavior:1.0.0'
}
Usage
You should have CoordinatorLayout in parent view. Add tag app:layout_behavior="io.apiqa.android.verticalswipe.VerticalSwipeBehavior" to your banner. Padding or margin is optional for positioning. View top is a start point for animation.
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/drag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/drag_me"
app:layout_behavior="io.apiqa.android.verticalswipe.VerticalSwipeBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Set sideEffect, clamp and settle into VerticalSwipeBehavior for customize banner movement.
val drag = findViewById<View>(R.id.drag)
VerticalSwipeBehavior.from(drag).apply {
sideEffect = NegativeFactorFilterSideEffect(AlphaElevationSideEffect())
clamp = BelowFractionalClamp(minFraction = 3f)
settle = SettleOnTopAction()
}
Details
SideEffect– Changes view properties depending on the progress of movement. Default implementationAlphaElevationSideEffectchange alpha and elevation of view. You may useNegativeFactorFilterSideEffectfor simple composition of behavior.WithoutSideEffectdoes not change any properties of view.PropertySideEffect- common way for changing several properties of view.
class AlphaElevationSideEffect: SideEffect {
override fun apply(child: View, factor: Float) {
child.elevation = elevation * (1f - abs(factor)) // special for elevation-aware view
child.alpha = 1f - abs(factor)
}
}
VerticalClamp– Have limits on moving view vertically. It is often necessary to limit movement to a part of the height of view. This is done byFractionClamp. Also you can useSensitivityClampfor tuning sensitivity.
class FractionClamp(private val maxFraction: Float = 1f, private val minFraction: Float = 1f): VerticalClamp {
override fun constraint(height: Int, top: Int, dy: Int): Int {
val min = min(top, originTop + (height * minFraction).toInt())
return max(min, originTop - (height * maxFraction).toInt())
}
override fun downCast(distance: Int, top: Int, height: Int, dy: Int): Float {
return distance / (height * maxFraction)
}
override fun upCast(distance: Int, top: Int, height: Int, dy: Int): Float {
return distance / (height * minFraction)
}
}
PostAction– Responsible for changing view position after swipe is completed. ViewDragHelper is used for animation moving of view in consideration of pointer speed.OriginSettleActionmoves the view to the starting position.
class OriginSettleAction: PostAction {
override fun releasedBelow(helper: ViewDragHelper, diff: Int, child: View): Boolean {
return helper.settleCapturedViewAt(child.left, originTop)
}
override fun releasedAbove(helper: ViewDragHelper, diff: Int, child: View): Boolean {
return helper.settleCapturedViewAt(child.left, originTop)
}
}
