[UI] Fix closing bottom sheet on scrim view click

This commit is contained in:
Kuba Szczodrzyński 2024-07-08 15:39:07 +02:00
parent 8450f6953e
commit ffee78d4f7
No known key found for this signature in database
GPG Key ID: 43037AC62A600562
2 changed files with 16 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.PorterDuff import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter import android.graphics.PorterDuffColorFilter
import android.util.AttributeSet import android.util.AttributeSet
import android.view.GestureDetector
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
@ -38,7 +39,8 @@ class NavBottomSheet @JvmOverloads constructor(
private val list private val list
get() = b.bsList get() = b.bsList
private var bottomSheetBehavior = BottomSheetBehavior.from<View>(bottomSheet) private val bottomSheetBehavior = BottomSheetBehavior.from<View>(bottomSheet)
private val gestureDetector = GestureDetector(context, OnGestureListener())
private val items = ArrayList<IBottomSheetItem<*>>() private val items = ArrayList<IBottomSheetItem<*>>()
private val adapter = BottomSheetAdapter(items) private val adapter = BottomSheetAdapter(items)
@ -58,10 +60,6 @@ class NavBottomSheet @JvmOverloads constructor(
WindowInsetsCompat.CONSUMED WindowInsetsCompat.CONSUMED
} }
scrimView.setOnClickListener {
isOpen = false
}
var bottomSheetVisible = false var bottomSheetVisible = false
bottomSheetBehavior.addBottomSheetCallback(object : bottomSheetBehavior.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() { BottomSheetBehavior.BottomSheetCallback() {
@ -187,6 +185,8 @@ class NavBottomSheet @JvmOverloads constructor(
} }
fun dispatchBottomSheetEvent(view: View, event: MotionEvent): Boolean { fun dispatchBottomSheetEvent(view: View, event: MotionEvent): Boolean {
if (view == scrimView)
gestureDetector.onTouchEvent(event)
val location = IntArray(2) val location = IntArray(2)
bottomSheet.getLocationOnScreen(location) bottomSheet.getLocationOnScreen(location)
event.setLocation(event.rawX - location[0], event.rawY - location[1]) event.setLocation(event.rawX - location[0], event.rawY - location[1])
@ -215,4 +215,12 @@ class NavBottomSheet @JvmOverloads constructor(
fun toggle() { fun toggle() {
isOpen = !isOpen isOpen = !isOpen
} }
inner class OnGestureListener : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent): Boolean {
isOpen = false
return super.onSingleTapUp(e)
}
}
} }

View File

@ -14,6 +14,9 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#99000000" android:background="#99000000"
android:visibility="invisible" android:visibility="invisible"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
tools:visibility="gone" /> tools:visibility="gone" />
<androidx.core.widget.NestedScrollView <androidx.core.widget.NestedScrollView