Created
April 22, 2019 12:38
-
-
Save tizisdeepan/04a636fd045e92d7964a27e60b2ebcca to your computer and use it in GitHub Desktop.
LockableBottomSheetBehavior can be used to lock a bottom sheet ignoring the swipe to dismiss actions done by the user.
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 com.google.android.material.bottomsheet.BottomSheetBehavior | |
import androidx.coordinatorlayout.widget.CoordinatorLayout | |
import android.util.AttributeSet | |
import android.view.MotionEvent | |
import android.view.View | |
class LockableBottomSheetBehavior<V : View>(context: Context, attrs: AttributeSet) : BottomSheetBehavior<V>(context, attrs) { | |
private var mLocked = false | |
fun setLocked(locked: Boolean) { | |
mLocked = locked | |
} | |
override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean { | |
var handled = false | |
if (!mLocked) { | |
handled = super.onInterceptTouchEvent(parent, child, event) | |
} | |
return handled | |
} | |
override fun onTouchEvent(parent: CoordinatorLayout, child: V, event: MotionEvent): Boolean { | |
var handled = false | |
if (!mLocked) { | |
handled = super.onTouchEvent(parent, child, event) | |
} | |
return handled | |
} | |
override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, nestedScrollAxes: Int): Boolean { | |
var handled = false | |
if (!mLocked) { | |
handled = super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes) | |
} | |
return handled | |
} | |
override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout, child: V, target: View, dx: Int, dy: Int, consumed: IntArray) { | |
if (!mLocked) { | |
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed) | |
} | |
} | |
override fun onStopNestedScroll(coordinatorLayout: CoordinatorLayout, child: V, target: View) { | |
if (!mLocked) { | |
super.onStopNestedScroll(coordinatorLayout, child, target) | |
} | |
} | |
override fun onNestedPreFling(coordinatorLayout: CoordinatorLayout, child: V, target: View, velocityX: Float, velocityY: Float): Boolean { | |
var handled = false | |
if (!mLocked) { | |
handled = super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY) | |
} | |
return handled | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
USAGE
(LockableBottomSheetBehavior instance).setLocked(true / false)