Add method findSuitableParent to get suitable parent view for custom snackbar

This commit is contained in:
Kirill Nayduik 2021-06-29 18:30:19 +03:00
parent b9d464605b
commit 1ee8f1b5a5
1 changed files with 31 additions and 0 deletions

View File

@ -2,6 +2,9 @@ package ru.touchin.extensions
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import ru.touchin.utils.ActionThrottler
const val RIPPLE_EFFECT_DELAY_MS = 150L
@ -22,3 +25,31 @@ fun View.setOnRippleClickListener(listener: () -> Unit) {
setOnClickListener { listener() }
}
}
/**
* Used for finding suitable parent view for snackbar
* Method was taken from com.google.android.material.snackbar.Snackbar.findSuitableParent
*/
fun View?.findSuitableParent(): ViewGroup? {
var view = this
var fallback: ViewGroup? = null
do {
if (view is CoordinatorLayout) {
return view
} else if (view is FrameLayout) {
if (view.id == android.R.id.content) {
return view
} else {
fallback = view
}
}
if (view != null) {
val parent = view.parent
view = if (parent is View) parent else null
}
} while (view != null)
return fallback
}