使用 android mozilla 移动 android 组件构建浏览器。我有一个动态顶部工具栏和一个动态底部工具栏覆盖 mozilla.components.concept.engine.EngineView (在幕后使用 Geckoview)。工具栏在引擎视图滚动时显示/隐藏。
问题是网页元素隐藏在本机工具栏后面。例如 youtube.com 主页具有基于 html 的动态顶部和底部工具栏,它们隐藏在我的本机工具栏后面。
mozilla.components.feature.session.behavior.EngineViewBottomBehavior 类更改操作方法告诉 engineview 在哪里放置元素。如果我的应用程序中有一个工具栏,这将有效。例如,如果我只有一个顶部工具栏,那么 topToolbarChangedAction() 设置垂直剪辑,并且 youtube.com html 顶部工具栏放置在我的本机工具栏下方。但是,当我有 2 个工具栏(底部和顶部)需要同时定位顶部和底部的 html 元素时,我不知道如何解决。下面是来自 mozilla 行为类的代码片段,其调用如下
(getSwipeRefreshLayout().layoutParams as CoordinatorLayout.LayoutParams).behavior =
EngineViewBrowserToolbarBehavior(
context,
null,
getSwipeRefreshLayout(),
toolbarHeight,
toolbarPosition
)
/**
* A [CoordinatorLayout.Behavior] implementation that allows the [EngineView] to automatically
* size itself in relation to the Y translation of the [BrowserToolbar].
*
* This is useful for dynamic [BrowserToolbar]s ensuring the web content is displayed immediately
* below / above the toolbar even when that is animated.
*
* @param context [Context] used for various Android interactions
* @param attrs XML set attributes configuring this
* @param engineViewParent [NestedScrollingChild] parent of the [EngineView]
* @param toolbarHeight size of [BrowserToolbar] when it is placed above the [EngineView]
* @param whether the [BrowserToolbar] is placed above or below the [EngineView]
*/
class EngineViewBrowserToolbarBehavior(
context: Context?,
attrs: AttributeSet?,
engineViewParent: View,
toolbarHeight: Int,
toolbarPosition: ToolbarPosition
) : CoordinatorLayout.Behavior<View>(context, attrs) {
private val engineView = engineViewParent.findViewInHierarchy { it is EngineView } as EngineView?
private var toolbarChangedAction: (Float) -> Unit?
private val bottomToolbarChangedAction = { newToolbarTranslationY: Float ->
engineView?.setVerticalClipping(-newToolbarTranslationY.toInt())
}
private val topToolbarChangedAction = { newToolbarTranslationY: Float ->
// the top toolbar is translated upwards when collapsing-> all values received are 0 or negative
engineView?.let {
it.setVerticalClipping(newToolbarTranslationY.toInt())
// Need to add the toolbarHeight to effectively place the engineView below the toolbar.
engineViewParent.translationY = newToolbarTranslationY + toolbarHeight
}
}
init {
toolbarChangedAction = if (toolbarPosition == ToolbarPosition.TOP) {
topToolbarChangedAction
} else {
bottomToolbarChangedAction
}
}
@SuppressLint("LogUsage")
override fun layoutDependsOn(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
// This package does not have access to "BrowserToolbar" ... so we are just checking the class name here since
// we actually do not need anything from that class - we only need to identify the instance.
// Right now we do not have a component that has access to (concept/browser-toolbar and concept-engine).
// Creating one just for this behavior is too excessive.
if (dependency::class.java.simpleName == "BrowserToolbar") {
return true
}
return super.layoutDependsOn(parent, child, dependency)
}
/**
* Apply vertical clipping to [EngineView]. This requires [EngineViewBrowserToolbarBehavior] to be set
* in/on the [EngineView] or its parent. Must be a direct descending child of [CoordinatorLayout].
*/
override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
toolbarChangedAction.invoke(dependency.translationY)
return true
}
}
/**
* Where the toolbar is placed on the screen.
*/
enum class ToolbarPosition {
TOP,
BOTTOM
}